0

Please help me with the number format in php for example I have some calculations, like a+b=c I want answer as 999.99 format in php I've read the number_format,sprintf but its not useful. please give me some useful ideas. My final result should be in 999.99 format.

rss
  • 61
  • 1
  • 2
  • 4
  • a,b,c are sample digits like 1+2=3 i want result 3 to be shown in 999.99 format or you can say in 003.00 format. another example 10+1=11 then it should be 011.00 format. – rss Feb 05 '13 at 10:32

6 Answers6

11

Per the 'not useful' documentation:

$num = 999.98353;
$num = number_format($num, 2, '.', '');

If you take the time to actually read the documentation, there are pertinent examples.

Julio
  • 2,261
  • 4
  • 30
  • 56
  • If a+b=c, even though c is a single/double digit with out decimal point (for example 1,2,5 etc) i should show c as 00c.00 if its single digit, or 0cc.00 if its double digit and if its three digit number no need to add one zero before the number. I hope my question reached you. I want to show my total in CANADA money format. Please help me in this regard of money format – – rss Feb 05 '13 at 10:30
  • number_format does that form you. Alternatively see http://php.net/manual/en/function.money-format.php. Read the docs . . . – Julio Feb 05 '13 at 14:18
3

In addition to number_format, you can use sprintf

$a = 999;
$b = .99;
$c = $a + $b;
echo sprintf('%03.2f', $c); // 999.99
dryer70
  • 53
  • 3
  • a,b,c are sample digits like 1+2=3 i want result 3 to be shown in 999.99 format or you can say in 003.00 format. another example 10+1=11 then it should be 011.00 format. – rss Feb 05 '13 at 10:38
  • Use str_pad to left pad with zero to the desired length:`$a = 1; $b = 2; $c = $a + $b; echo str_pad(sprintf('%.2f', $c), 6, "0", STR_PAD_LEFT); // 003.00` – dryer70 Feb 05 '13 at 16:50
  • Thank you so much for your reply.it really helped me a lot. I did it in another method and it worked, $a=1;$b=2;$c=$a+$b; $final_result = (strlen(number_format((float)$c, 2, '.', '')) < 6) ? '0'.number_format((float)$result, 2, '.', '') : number_format((float)$result, 2, '.', ''); echo ''.$final_result.''; digits may be single or double it'll give the desired result i.e 999.99 format. Thanks for your time. it was a great help. – rss Feb 06 '13 at 06:16
1
$num = 999.98353;
$num1 = 10;
$num2 = $num + $num1;
echo number_format($num2, 2, '.', '');   // output 1009.98
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • If a+b=c, even though c is a single/double digit with out decimal point (for example 1,2,5 etc) i should show c as 00c.00 if its single digit, or 0cc.00 if its double digit and if its three digit number no need to add one zero before the number. I hope my question reached you. I want to show my total in CANADA money format. Please help me in this regard of money format – rss Feb 05 '13 at 10:37
1

try this

$a = 500.3755;
$b = 600.9855;
$c = $a + $b;
echo number_format($c, 2, '.', ''); //1101.36
echo number_format($c, 2, '.', ','); //1,101.36
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
1

You can use

$c = sprintf('%0.2f',$a)+sprintf('%0.2f',$b);

Or you can also try :

$c = $a+ $b; // If $c = 999.9999
$c = substr($c, 0, 2); // $c is now the string 999.9999
echo( number_format($c-1,2) );  // Will output 998.99
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53
Hemali
  • 11
  • 2
  • If a+b=c, even though c is a single/double digit with out decimal point (for example 1,2,5 etc) i should show c as 00c.00 if its single digit, or 0cc.00 if its double digit and if its three digit number no need to add one zero before the number. I hope my question reached you. I want to show my total in CANADA money format. Please help me in this regard of money format – rss Feb 05 '13 at 10:20
  • I did it in another method and it worked, `$a=1;$b=2;$c=$a+$b; $final_result = (strlen(number_format((float)$c, 2, '.', '')) < 6) ? '0'.number_format((float)$result, 2, '.', '') : number_format((float)$result, 2, '.', ''); echo ''.$final_result.'';` digits may be single or double it'll give the desired result i.e 999.99 format. Thanks for your time. it was a great help. – rss Feb 06 '13 at 06:19
1

    $a = 1234.5678;
    $b = 1234.5678;
    $num = round(($a + $b), 2);
    echo number_format($num, 2, '.', ',');

number_format works, try posting some of your codes.

khatzie
  • 2,509
  • 4
  • 22
  • 37
  • If a+b=c, even though c is a single/double digit with out decimal point (for example 1,2,5 etc) i should show c as 00c.00 if its single digit, or 0cc.00 if its double digit and if its three digit number no need to add one zero before the number. I hope my question reached you. I want to show my total in CANADA money format. Please help me in this regard of money format – rss Feb 05 '13 at 10:29