View Full Version : How 1,5 + 1 = 2,5 Not 2
Shadowfox
02-24-2006, 09:59 PM
HOW 1,5 + 1 = 2,5 NOT 2
Should I use some special function for PRECISE mathematical calculation?
marek_mar
02-24-2006, 10:04 PM
Huh? Call me an idiot but 1,5 (or 3/2) + 1 (or 2/2) is indeed equal to 2,5 (or 5/2) and not 2 (4/2). And thats quite percise.
Shadowfox
02-24-2006, 11:16 PM
Dude, I'm sorry if I haven't made myself clear.
I ask PHP to make a calculation for me:
$a = 1,5
$b = 1
$c = $a + $b
in this case $c = 2 which is not 2,5
What I ask you is how to make PHP calculate this precisely and the echo of my $c variable should be 2,5 not 2.
slushy77
02-24-2006, 11:50 PM
Hi Shadowfox,
try this
$a= (float) "1.5";
$b = (float) "1";
$c = $b +$a;
Spookster
02-25-2006, 12:09 AM
Run this:
<?php
$a = 1,5;
$b = 1;
$c = $a + $b;
echo $c;
?>
You should see 2.5 echoed to the screen. Also post the exact code you are using where you are getting 2.
dumpfi
02-25-2006, 05:28 PM
When typing floating point literals use "." instead of ","
<?php
$a = 1.5;
$b = 1;
$c = $a + $b;
echo $c;
?>dumpfi
Digger3000
02-25-2006, 06:38 PM
HOW 1,5 + 1 = 2,5 NOT 2
Should I use some special function for PRECISE mathematical calculation?
What are you talking about? What do 1,5 and 2,5 mean?
gsnedders
02-25-2006, 06:41 PM
What are you talking about? What do 1,5 and 2,5 mean?
The character used as the decimal point isn't the same everywhere.
Shadowfox
02-25-2006, 07:24 PM
Okay Dumpfi, could you provide me with a REGEX expression which changes all "," characters in a file (.txt) with "."
NancyJ
02-25-2006, 07:31 PM
you dont need a regex expression, just str_replace() to change all the commas to decimal points.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.