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 ?

View 5 Replies


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

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

$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

Reducing The Result

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

Reducing Fractions..

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

Reducing Sql Value By One Each Time

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

Reducing Server Load

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

Reducing Field Lengths

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

Reducing Code Clutter

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

Reducing Memory Consumption

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

Reducing Sql Server Load

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

Reducing GIF Filesize Using ImageMagick?

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

$anim = new Imagick();
for($i = 0; $i<=36; $i++) {
$bgImage = new Imagick('Background.gif');
$imagick = new Imagick("sw_layers-$i.gif");


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

Reducing Processing Time

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

Images Not Reducing In Size

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.


View 6 Replies

Reducing The Number Of Collors For Image...

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

Atomicity In Map Reducing Over New Records (MongoDB)?

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

Reducing An Image File Size?

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

Shopping Cart On Reducing Balance?

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

Make Code Small While Not Reducing Performance?

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:


does this make it all work slower?

View 7 Replies

Reducing Duplication In Static HTML Pages?

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




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


<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


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

Reducing Number Of Includes / Requires Increase Performance?

What is better practice for script performance tuning?

This one?
if (!is_cached()) {
//do something
} else {
//display something.............

View 5 Replies

Reducing Size Of User Avatars - Creating Thumbnails?

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

Numbers And Decimals

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

Handling Decimals

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

Remove All Decimals But Last?

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

Random With Decimals

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?


View 1 Replies

Copyrights 2005-15, All rights reserved