...

View Full Version : function format_number



FatCodeMonkey
07-24-2008, 05:44 PM
I have an order form that originally had 4 digits after the decimal place. We changed it to 2, but now the price rounds up (for example, if the price is $65.34, it shows $66.00 on the order form - but once the data is transferred to the database, it shows the exact dollar amount.)

What do I need to change in this code in order for the price not to round up on the order form?

function format_number($str,$decimal_places='2',$decimal_padding="0"){
$str = number_format($str,$decimal_places,'.',''); // will return 12345.67
$number = explode('.',$str);
$number[1] = (isset($number[1]))?$number[1]:''; // to fix the PHP Notice error if str does not contain a decimal placing.
$decimal = str_pad($number[1],$decimal_places,$decimal_padding);
return (float) $number[0].'.'.$decimal;
}

Thanks in advance for your assistance.

Iszak
07-24-2008, 07:33 PM
I did a quick remake of yours.. erm here it is.



<?php

function format_number($number, $decimal = "2", $padding = "0")
{
if ($decimal != -1)
{
$number = number_format($number, $decimal, '.', ' ');
}

if ($decimal == 0 && $padding != 0)
{
$number .= ".";
}

$number = str_pad($number, strlen($number)+$padding, "0");
return $number;
}

echo format_number("65.3456", -1)."<br />"; // Output = 65.3456
echo format_number("65.3456", 0, 2)."<br />"; // Output = 65.00
echo format_number("65.3456", 2)."<br />"; // Output = 65.35
echo format_number("65.3456", 0)."<br />"; // Output = 65

?>


I think you get the idea...

oesxyl
07-24-2008, 08:04 PM
you can use sprintf:



$value = 63.1456;
// padding with 0, minim width 1, 2 decimals
echo sprintf("%01.2f",$value);


http://www.php.net/manual/en/function.sprintf.php

regards

FatCodeMonkey
07-24-2008, 09:41 PM
Hi Iszak,
First of all, thank you for your willingness to help. It is deeply appreciated.

When I substituted the old code for what you had, not only did it place the output numbers at the top of the order form page, but it also deactivated the database query to pull in the prices. Why is this?

Iszak
07-24-2008, 09:53 PM
We'll have to see the code. But the output at the top of the page wouldn't happen.. unless you added the echo parts.. which was just an example.. As for the deactivation of the database queries I don't see why that would happen.. again we need the code.

FatCodeMonkey
07-24-2008, 10:50 PM
Iszak, here is the original code:



<?php

//mysql_connect ('localhost', 'root', 'pw');
//mysql_select_db('dbname')



mysql_connect("localhost","root","pw");
mysql_select_db("dbname");


function format_number($str,$decimal_places='2',$decimal_padding="0"){
/* first format number and shorten any extra decimal places */
/* Note this will round off the number pre-format $str if you dont want this functionality */
$str = number_format($str,$decimal_places,'.',''); // will return 12345.67
$number = explode('.',$str);
$number[1] = (isset($number[1]))?$number[1]:''; // to fix the PHP Notice error if str does not contain a decimal

placing.
$decimal = str_pad($number[1],$decimal_places,$decimal_padding);
return (float) $number[0].'.'.$decimal;
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum