Reducing Decimals

Aug 18, 2007

Doing a simple division, I obtain this number :
0.002409638554216867234458732127677649259567260742 1875
I'd like to have 0.0024 instead.
I know the number_format function but is it the right solution ?
I thought of another solution :

$MyDec = 0.002409638554216867234458732127677649259567260742 1875;
$MyDecOk = round($MyDec * 10000) / 10000 ; # =0.0024

Is there a special function to do so ?

ADVERTISEMENT

Javascript - Print A Number With 2 Decimals, But Only If There Are Decimals Already?

Apr 13, 2011

I have a basic index.php page with some variables that I want to print in several places - here are the variables:

<?php
$firstprice = 1.50;
$secondprice = 3.50;
$thirdprice = 20;
?>

My challenge is that later in the document, when I print, I get the prices without the second '0' in the price - this is what happens:

<?php print "$firstprice";?> // returns 1.5 - not 1.50!

SO - I know how to do this with JS, but how is this done in PHP 5+? Basically I want to print the second '0' if there is already a decimal, so if the variable is equal to '3', it stays as '3', but if it's equal to '3.5' it converts to display '3.50' with a second '0', etc.

Here's a JS example - what's the PHP equivalent?

JS:.toFixed(2).replace(/[.,]00$/, ""))

View 5 Replies View Related

Reducing The Result

Jun 2, 2006

I have a text field for a news story. What I would like to do is call two versions of the field $story. One is the complete $story field, the other is the $story field, but limited to the first 15 words in the $story field. How can I do this?

View 6 Replies View Related

Reducing Fractions..

Mar 19, 2006

I am wandering if there is a way to reduce fractions? I have created a script that finds the answers to an algebra problem (that uses Cramers Rule).. and the only thing, is that the fractions aren't reduced.

Yeah, its bad enough that I am actually making this, and sure, I should probably reduce the fractions myself, but I might possibly need to know the function in the future (if there is one).

View 2 Replies View Related

Reducing Sql Value By One Each Time

Nov 25, 2010

I have an SQL table with a quantity in and I am trying to write a piece of code to reduce the quantity by 1 each time. Here's my code:

$queryc = mysql_query(" SELECT FROM `voucher_codes` WHERE name = '".$voucherc."'");
$resultc = mysql_fetch_array ($queryc);
$voucherqtyvalue = $resultc['qty'];
$newvoucherqty = $voucherqtyvalue - 1;
$queryi = mysql_query(" UPDATE `voucher_codes` SET qty = '".$newvoucherqty."' WHERE name = '".$_SESSION['vouchercode']."'");
$resulti = mysql_result($queryi);

Instead, this changes the value to -1 in the table.

View 3 Replies View Related

Reducing Server Load

Jul 29, 2003

I've got a script that I've released as open source. A user recently reported to me that their host has complained that it's using too much of the server resources. He says that it occasionally jumps up to 15% memory usage, and the CPU usage up around 1.5-2.5. This is on a Celeron 2.2gHz with 512 of RAM. He reports getting around 900 unique visitors a day.

I'd like to do what I can to improve the script to make it not so heavy on server load, but am not quite sure what to do to improve it. All of the appropriate fields are indexed, and when doing queries, I always call the specific columns and don't use SELECT *. Anyone got any other suggestions?

View 1 Replies View Related

Reducing Field Lengths

Jul 17, 2005

Initially when I setup my PHP and MySQL site I didn't know much about table
design. One of the "fixes" I am trying to do is reduce the size of the
fields to the length of the largest entry.

Is there an easy script that I can use for each table I have to do this ?
Some kind of global ALTER ?

View 3 Replies View Related

Reducing Code Clutter

Jul 17, 2005

Newish PHP programmer here. I wrote a form select class to help reduce code
clutter, but I don't think the effort was worth it. I was hoping to post my
attempt and get some ideas from more advanced users on how to implement a
select form using less lines of code.

First the select form class which implements a select widget. I'll post only
the relevent parts:

class frmSelectClass
{
// Attributes
var $name = ''
var $size = ''
var $multiple = ''
var $id = ''
var $class = ''
var $style = ''
// Options
var $options = array();

function frmSelectClass($arg = '')
{
if ( gettype($arg) == 'array' )
{
foreach ( array_keys($arg) as $key )
{
if ( $key == 'name' )
$this->name = 'name="' . $arg['name'] . '" '
elseif ( $key == 'size' )
$this->size = 'size="' . $arg['size'] . '" '
elseif ( $key == 'multiple' )
$this->multiple = 'multiple="' . $arg['multiple'] . '" '
elseif ( $key == 'id' )
$this->id = 'id="' . $arg['id'] . '" '
elseif ( $key == 'class' )
$this->class = 'class="' . $arg['class'] . '" '
elseif ( $key == 'style' )
$this->style = 'style="' . $arg['style'] . '" '
}
}
}

View 7 Replies View Related

Reducing Memory Consumption

Apr 24, 2007

I'm using PHP to run a CLI application. It's a script run by cron that parses some HTML files (with DOM XML), and I ended up using PHP to integrate with the rest of the code that already runs the website. The problem is: it's eating more memory than a black hole. It eats the current limit of 256MB set in php.ini, in an application that would hardly consume 4MB if written in C. I don't care if this application takes much longer
to run than it would in C, but eating that much memory is not acceptable.

View 9 Replies View Related

Reducing Sql Server Load

Aug 26, 2006

Sometimes my website just stops running and only displays errors (see below). Is there a way i can fix the problems?

Should it help if i use mysql_close() anywere? Also : how to use unset() and mysql_free_result (which,when) to reduce sql server load? Code:

View 12 Replies View Related

Reducing GIF Filesize Using ImageMagick?

Mar 25, 2011

I am writing some code using PHP and Imagick which gathers multiple images into one animated GIF using the following code:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$anim = new Imagick();
for($i = 0; $i<=36; $i++) {
$bgImage = new Imagick('Background.gif');
$imagick = new Imagick("sw_layers-$i.gif");

[Code]...

The background image and "Hello world" are supposed to appear for every frame. As you can see i am joing the text image, the background image and the layer into a single frame. the layer image contains some kind of a drawing with a transparent background so my supplied background and text should appear in that case. However, the problem lies in the filesize of the animated gif "result.gif" which turns out to be around 3 MB for a 30 frames image.

The question is, How can i reduce the filesize without affecting the quality that much? i am open to answers including running a commandline tool on the system.

View 1 Replies View Related

Reducing Processing Time

Nov 6, 2007

I've written the page below which creates a table of sales figures from an sql database, unfortunately the way I have written it if someone has 40 customers the database hase to run about 520 select queries. Can anyone see a more streamlined way of doing this?

What I am basically trying to get is a table with these columns:

Customer ID, Jan, Feb, Mar, April, May ..... Dec, Year-To-Date

Under the YTD and month columns there is just a quantity and total value. Anway here's the code if anyone can help I'd appreciate it as every time someone has a look at their sales page the whole webserver locks up! Code:

View 14 Replies View Related

Images Not Reducing In Size

Oct 18, 2010

I'm using the script below to that allows a user to upload a photo, which then appears on the page as a thumbnail, the image can be clicked and opens up in a lightbox image pop up. All this works fine except the images are not resizing. I.e if a a picture is 1024px wide, before upload - it stays that size on the server.I need it to compress down to around 600px wide and have the KB of the file reduced. I know that I should be using GD library to do this but not sure how to implement it.

[Code].....

View 6 Replies View Related

Reducing The Number Of Collors For Image...

Apr 10, 2006

Basically, what I want to do is to create a thumbnail that will be available for download for mobile phones background and I want to save my users as much bandwidth as possible...

As I understand by reducing the collor depth by small amount I could get significantly smaller images than by simply using imagesopyresampled.

View 2 Replies View Related

Atomicity In Map Reducing Over New Records (MongoDB)?

Jun 30, 2011

I've got a MongoDB cluster and a web-app, which does a pretty intensive Map/Reduce query. This query happens periodically (every 5min) in a cron job, and the results are stored (using $merge) into a collection.What works: Currently, the query performs over every record in its collection. Said collection is slowly growing to be millions of rows, and each time it runs, it takes a little longer.The obvious solution is to run the Map/Reduce over new records, and use the reduce function over the old stored values to calculate the correct value. MongoDB is great, it lets you specify a reduce option instead of merge to do just that.What I can't figure out: How to correctly perform the M/R only over new records in the initial collection. I see two potential solutions, neither of which are good. Ideas?I could flag records that have been processed. Problem is how to flag exactly the same records that I just M/R'd over?I could query for the matching items, then pass the list of ids as an $in: [id1, id2, ...] query to the Map/Reduce, and then send an update to set my flag using the same $in. But that's really inelegant, and I don't know how that's going to perform when the list of records is huge.

View 1 Replies View Related

Reducing An Image File Size?

May 17, 2007

im aware there is a method which enables you to reduce the file size of an image using PHP. This is obviously great for creating smaller sized, fast loading thumbnails of a larger image without having to manipulate the image yourself at all. Great for people who dont really know anything about image manipulation too!

Ok then so my question is: "Does anyone know how this is done!?"

Or can anyone suggest a better way to do this without having to manipulate the image yourself and then upload it to the server before it can be accessed.

View 10 Replies View Related

Shopping Cart On Reducing Balance?

May 27, 2011

Does anybody have links or scripst on a script where the user has an account with money which decreases as one purchases items.

View 2 Replies View Related

Make Code Small While Not Reducing Performance?

Jul 21, 2009

I'm writing a Content Management System in PHP, and I want it to be the smallest one in the world. I'm planning to make it available to everyone, just like Drupal and Joomla. But to make it so ultra-tiny, I change code to smaller code. For example, I change:

$info = parse_ini_file("info.scm"); /* to */ $i=parse_ini_file("info.scm");

just to make it smaller. But, I use some functions very often, like preg_replace();. I use it over 30 times. Should I make a function like:

[CODE....]

does this make it all work slower?

View 7 Replies View Related

Reducing Duplication In Static HTML Pages?

Feb 7, 2009

I have a website which consists of a bunch of static HTML pages. Obviously there's a lot of duplication among these (header, menu, etc). The hosting company I plan to use supports PHP, which I know nothing about. Presumably PHP provides some sort of #include mechanism, but what changes to I need to make to my HTML pages to use it? For example, suppose I have a page like this

index.html

<html>
<head></head>
<body>

[code]....

Obviously I need to move the common part into it's own file:

header.html

<h1>My Common Header</h1>

Given the example above (and assuming all files are in the same directory): What do I add within the body tag to get header.html included? Do I need to rename index.html or add some special tags to indicate that it's a .php file? Do I need to make any changes to header.html?

Update: I want to emphasise that my objective here is simply to find the lowest-friction means of reducing duplication among static HTML files. I'm a bit reluctant to go down the server side includes route because I don't yet know what type of server (IIS/Apache) I'll be hosting the files on, and whether includes will be turned on or off. I was drawn towards PHP only because it is about the only thing I can presume will be available that will be able to do the job.

View 8 Replies View Related

Decimals

Feb 8, 2007

What i need to do is display mysql results through a table, my problem is that if i am short on results it wont add in the remaining <td>'s to fill the previous rows. I have got the code sorted down to just needing to be able to read a decimal. If the decimal is .66 when the number of rows is divided by 3 then i need to add in another <td> and add another 2 when the decimal is .33. help me recognise this from a variable?

View 1 Replies View Related

Reducing Number Of Includes / Requires Increase Performance?

Aug 6, 2010

What is better practice for script performance tuning?

This one?
require_once("db.php");
if (!is_cached()) {
require_once("somefile.php");
require_once("somefile2.php");
//do something
} else {
//display something.............

View 5 Replies View Related

Reducing Size Of User Avatars - Creating Thumbnails?

Sep 27, 2009

User of my site upload images which are used as avatars. I have set a upload limit size of 2 MB. At most places I only require thumbnails. But users upload images with far bigger resolutions. I store these files on my file-system. How can i create thumbnails and store them instead of large sized files?

View 7 Replies View Related

Numbers And Decimals

Apr 16, 2003

I'm trying to get this 124110 to look like this 124.11 . Here is the code I'm using to get my numbers PHP Code:

View 2 Replies View Related

Handling Decimals

Aug 25, 2006

I am looking for an easy way of handling decimal places. I only need the right most 2 decimal places.

For example. If I divide 1 by 3, my result would be .3333333333, In this case, I would only want .33 or .333, is there some kind of PHP command or function call that would make it easy to do this? Or will I have to write my only routine?

View 4 Replies View Related

Remove All Decimals But Last?

Jun 11, 2011

I would like to know of a way to remove all decimals from a files name but that last one. I need to keep the last one because the extension follows that.EXAMPLE:abc.def.ghij-klmnop.q234.mp3This file should look likeabcdefghij-klmnopq234.mp3Some extensions are longer than 3 char.

View 4 Replies View Related

Random With Decimals

Mar 18, 2007

how would i make it so that this script recognizes that the random numbers are with 2 decimal places, and make it so it doesnt just round them up?

$randomattack=rand(.95,1.05);
$randomdefence=rand(.95,1.05);

View 1 Replies View Related

Formatting Decimals

Aug 24, 2007

I have this line.

$form .= "<td>£$price[$i] ex VAT
<br />£".$price[$i]*1.175." inc VAT</td>";
this returns

£41.95 ex VAT
£49.29125 inc VAT

I would like the inc VAT price to become £49.29. I have tried

$form .= "<td>£$price[$i] ex VAT
£".printf("%.2f", ($price[$i]*1.175))." inc VAT</td>";

But this doesn't work.

View 2 Replies View Related

Format Currency With More Than 2 Decimals

Aug 3, 2003

I've tried to use both round and money_format but I can't seem to get what i what.

I have a shopping cart on my web site and I had the tax to the subtotal for calculating the total.

However the following problem arises when using round():
$13.2818
becomes $13.28 (should be $13.29)

$13.2772
becomes $13.28 (works fine)

Is there another function that will ALWAYS round up after X number of decimals?

View 4 Replies View Related

Formatting Of Decimals In A Table.

Aug 15, 2002

I'm not sure how to format :

15.00
155.00

so the .00 part lines up in a cell row I'm using a while loop to display the prices from an array. How do I get them to line up?

View 3 Replies View Related

Function To Strip Decimals

Jun 25, 2007

I'm trying to calculate interest for someones points in my bank system, however i don't want any decimals.

I have used:

number_format($interest, 0);

but when someone gets 1,000 interest, it removes the three zeros. Any help?

View 2 Replies View Related

Rounding Or Truncating Decimals?

Jun 20, 2007

is there a way to round or truncate a calculation to 2 decimals places? Accuracy is not entirely important so if truncating is easier I am ok with that. Here is the code I want to work with? I did read some about having to use a java script to round off but I couldn't figure it out.

$budremain = $invsub / 1.06 ;
echo '<td nowrap colspan = 2 align = "right"> $<b>' . $budremain . '</td></tr></b>'

View 2 Replies View Related

Format Number 2 Decimals

Dec 16, 2005

help me with the php code to format a number with 2 decimals.

View 1 Replies View Related

Math Functions With Decimals

Jul 12, 2007

I'm trying to display a decimal number and i want it to be displayed with 2 decimal numbers always. e.g. 5.95. the problem with this is that when the number is 10.00 it will be displayed as 10. is there a way to fix this?

View 3 Replies View Related

Handling Decimals With Operators

Jul 17, 2005

I am trying to make a query that will not return any item larger than 3.000 in a width field, but I have an item returning with a width of 7.500

I am wondering if it is a function of a 3 place decimal field. Here is the query:

SELECT *
FROM `Catalog`
WHERE `Catalog`.designer LIKE 'Victorian' AND `Catalog`.width <3.000
ORDER BY `Catalog`.sold

I've tried it with single quotes, but the item still comes up.[color=blue]
> AND `Catalog`.width <&#393;.000'[/color]

What is weird is that the designer field has nothing like Victorian in it,
but it still pops up.

View 2 Replies View Related

Convert Lat / Lng String To Decimals

Aug 28, 2009

I would like to convert a lat/lng string to decimals in php. An example:

N47 30.5951' = 47+(30.59517/60) = 47.509918333

Is there any easy way to do that, regular expression?

View 3 Replies View Related

Round Decimals To Nearest .5?

Jun 22, 2010

Is it possible to round a decimals to the nearest .5 with PHP like this:

number | round
----------------
1.29 | 1.50
2.03 | 2.00
1.43 | 1.50
1.13 | 1.00
1.38 | 11.50

I tried with:

$rnd= round($number,2);

but I get decimals like the one in the column "number" above.

View 1 Replies View Related

Database - Removing All Decimals?

Feb 4, 2011

get this from my database:

252.587254564

Well i wanna remove the .587254564 and keep the 252, how can i do that?

What function should i use and can you show me an example?

View 8 Replies View Related

Regex Replace For Decimals?

Jul 17, 2009

I'm trying build a regex that will replace any characters not of the format:any number of digits, then optional (single decimal point, any number of digits)

i.e.
123 // 123
123.123 // 123.123

[code]...

View 2 Replies View Related

Php5 - Round Up The Value Up To 2 Decimals?

Sep 3, 2010

iam doing division of these two columns $row['col1']/$row['col2'] iam getting value as 0.0042553191489362

How do i round up the value so that i can get as 0.42%

View 1 Replies View Related

Price Column - 2 Decimals?

Mar 10, 2010

i have a field in the db that is set to 'float' which holds a price, but when you enter say 444.50.... it saves it as 444.5 and therefore displays on the page as 444.5. Do i need to change the field type to something else? or can i just rewrite this below so it shows 2 decimals, if so how?

<?php echo $row_jobs['price']; ?>

View 4 Replies View Related

Force A Number To Have 2 Decimals

Sep 4, 2007

I have a query, and one of the results is a number that I want to forcefully have rounded to 2 decimal places. I couldn't find a good search query to find this if it's been asked before How can I achieve it, so it's like 1.00 or 187.25

View 5 Replies View Related

Calculation And Limiting Decimals

Oct 4, 2007

I have a greater total of 61 and I score 45

$i= 61;
$y= 45;

$pct = ($i /$y) * 100;

one is tha correct Two how can i limit the number that result diplays (0.987654321 - i just want .987)

and how do i turn off this error message???

Warning: Division by zero in localhostinfo.php on line xxx

View 5 Replies View Related

Limiting A Variable To Only 2 Decimals (.XX)??

May 8, 2007

If I have a multiplication calculation for a PHP variable, how can I set it to only limit to 2 decimal places (.XX). I'm passing the amount value to a merchant account and it's kicking it back if it's over the 10th decimal spot.

View 2 Replies View Related

Only Display Decimals If They're Present?

Sep 27, 2010

ode that asks for a column called "distance_completed" to be displayed as part of a string.This column tells users how far people have run in a running race.I set up this field in my database to 11,3 so all distances appear with a decimal at the end, and then 3 digits after the decimal place.How would I alter the code so that the numbers that don't have any information in the places section just appear as integers Eg: 42 instead of 42.000 (for someone who ran exaclty 42km) and those that do have information in the decimal places appear correctly (eg: if a person ran 42km and 195m, it would appear as 42.195).I think this is probably the only line of code that would need modification:

Code: [Select]
echo ($row['time'] != null)?$row["time"]:"dnf at ".$row["distance_completed"]."km.";

View 4 Replies View Related

Command That Does Something Similar To Intval() But For Decimals?

Jul 26, 2010

What is the PHP command that does something similar to intval() but for decimals?

Eg. I have string "33.66" and I want to convert it to decimal value before sending it to MSSQL.

View 4 Replies View Related

ADVERTISEMENT