...

View Full Version : Problem in displaying number format with comma and two decimal places



newphpcoder
11-14-2011, 02:59 AM
Good day!

I use round syntax in displaying my computation and the result is correct. but it has no comma.
Like for example the result is 3020, I want it to 3,020.00

I have this syntax:



$Amount = round(($Hours/8)* $Rate, 2);


but when I tried number_format it displayed correct but I think the data or the amount is wrong because I sum amount in other earnings before i change the format, the output is 5524.28, but when I change my format of my amount the sum of other earnings and amount become 2507.88, so the data that get in amount is only 3.6.

i only want is adding comma, with changing the real amount or data.

Thank you so much....

myfayt
11-14-2011, 03:51 AM
You have to use number_format() such as this.


You have number_format($bankaccount) money in your account.

$Amount = round(($Hours/8)* $Rate, 2);
$Amount2 = number_format($Amount);


http://php.net/manual/en/function.number-format.php

newphpcoder
11-14-2011, 08:15 AM
You have to use number_format() such as this.


You have number_format($bankaccount) money in your account.

$Amount = round(($Hours/8)* $Rate, 2);
$Amount2 = number_format($Amount);


http://php.net/manual/en/function.number-format.php

I treis this, but still change the output number....the equal number of 3,200 is become 463 only.

before the total earnings is; 6056.55 and when I change the number format the basic pay from 3020, become 3,020 which is correct but when I sum to other deduction the output become 2788.13

myfayt
11-14-2011, 03:22 PM
number_format shouldn't affect the amount in any way. The code posted was an example, you'll have to determine the best way to do it. Another idea to try is this.


$Amount = number_format(round(($Hours/8)* $Rate, 2));

newphpcoder
11-15-2011, 01:21 AM
this is my code:


$sql = "SELECT w.EMP_ID, w.RATE, e.Hours FROM $ADODB_DB.wage w, $PAYROLL.earnings e WHERE w.EMP_ID = '$currentEmpID'";
$RsEarnings = $conn2->Execute($sql);

$Rate = trim($RsEarnings->fields['RATE']);
$Hours = trim($RsEarnings->fields['Hours']);

$Hours = substr($Hours, 0, 5);
$Hours = str_replace(':', '.', $Hours);


$Amount = number_format(round(($Hours/8)* $Rate, 2));


$TotEarn = number_format(round($Amount), 2);

rate = 302
hours: 57.55
amount = 2,173
totearn = 2.00 //wrong

Fou-Lu
11-15-2011, 07:28 PM
Um, number_format is a string. You are now trying to operate mathematically on a string which may produce improper results. This is especially true if you provide a , within the number, as its now directly a string which cannot be converted to a number. When it tries, it will truncate from the first invalid character, which is the comma from the thousand separator. Use number_format or money_format only on the very last result to display as a string. Alternatively, you may use [s]printf to format with the decimal points and commas, but will lose the locale knowledge of the currency in use (so you need to manually add a $ or £ for example).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum