开发者

How to do calculation of two values with commas?

I have two value and I want to do calculation of tho开发者_如何学Cse values.

For example :

$a = '750,000.00';
$b = '763,000.00';
echo $a + $b;

and I am getting the result 1513 which is wrong.. The output must be 1513000.

How to do such calculations?


You have to eliminate the commas before the strings are interpreted as float values:

$a = '750,000.00';
$b = '763,000.00';
echo str_replace(',', '', $a) + str_replace(',', '', $b);


Use str_replace:

// replaces all , with nothing
echo str_replace(',','',$a) + str_replace(',','',$b);


The following converts a String to Int:

<?php
$a = (int)'750,000.00';
$b = (int)'763,000.00';
echo $a + $b;
?>

Or String to Float as mentioned by Flinsch

<?php
$a = (float)'750,000.00';
$b = (float)'763,000.00';
echo $a + $b;
?>

However, you may want to take localisation into account (different writing of numbers in different parts of the world). My Belgian localisation settings outcome this as "1.513" instead of 1.513.000

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜