0
var uc = 2.10;
var dort_bucuk = 1.80;
islem  = parseFloat(uc-dort_bucuk);
alert(islem) // 0.30000000000000004

var uc = 1.10;
var dort_bucuk = 0.95;
islem  = parseFloat(uc-dort_bucuk);
alert(islem) // 0.15000000000000013

var uc = 4.00;
var dort_bucuk = 3.70;
islem  = parseFloat(uc-dort_bucuk);
alert(islem)//0.2999999999999998

how i use like in php number_format($int,2) why its coming 4.00-3.70 =0.2999999999999998? its must

<?php 
 $bir = 4.00;
 $iki = 3.70;
echo number_format($bir-$iki,2); // 0.30 
 ?>

thanks for helping

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • 2
    [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – Yoshi Jun 27 '12 at 11:07

1 Answers1

0

Try .toFixed(..), e.g.:

alert(islem.toFixed(2));

Also, note that you don't need any of the parseFloats, since you are subtracting two numbers, so the result is a number.

huon
  • 94,605
  • 21
  • 231
  • 225