Perl int function and padding zeros -


  1. i have below perl function display 2 decimals places. it's not working when input value 2.01, , gives output 2 instead of 2.01. not sure why it's rounding.

instead of printf wrote output file, still gives me output1 2.

    $ramount = 2.01;     $ramount = int($ramount*100)/100;     printf "output1: $ramount"; 
  1. if have values .2, .23, .2345, 1,23, 23.1, , 9, function can use pad zeros displays 0.2, 0.23, 0.2345, 1, 23, 23.1, , 9?

i think sequence answer question:

  db<1> $a=2.01    db<2> p $a 2.01   db<3> printf "%20.10f\n", $a         2.0100000000    db<4> printf "%20.16f\n", $a   2.0099999999999998    db<5> printf "%20.16f\n", ($a*100) 200.9999999999999716    db<6> printf "%20.16f\n", ($a*100)/100   2.0099999999999998    db<7> printf "%20.16f\n", int($a*100) 200.0000000000000000    db<8> printf "%20.16f\n", int($a*100)/100   2.0000000000000000    db<9> 

essentially (and has been answered many times on so), 2.01 cannot represented floating point number. closest possible float is, see above, 2.009999999999999716...

as padding, try

printf "%04d", $number 

the leading 0 in format tells printf (or sprintf) left-pad zero.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -