|
From: stevob on 27 Nov 2005 05:43 Hi all, I was wondering if it is possible in perl to get a number to have exactly two decimal places (like xx.xx). I read about Number::Format, but that is not available for me to use. I also know you can do a printf to display it, but I need it to be like that in the variable. i.e. if someone enters in 12.5, i need my variable that stores it to store 12.50 not 12.5. Same thing if it was 12, I would need it to be 12.00 stored. Is that possible? Thanks. Stevo
From: Brian Wakem on 27 Nov 2005 05:54 stevob(a)gmail.com wrote: > Hi all, > > I was wondering if it is possible in perl to get a number to have > exactly two decimal places (like xx.xx). I read about Number::Format, > but that is not available for me to use. I also know you can do a > printf to display it, but I need it to be like that in the variable. > i.e. if someone enters in 12.5, i need my variable that stores it to > store 12.50 not 12.5. Same thing if it was 12, I would need it to be > 12.00 stored. Is that possible? Thanks. perldoc -f sprintf my $i = sprintf "%.2f",12; print $i; -- Brian Wakem Email: http://homepage.ntlworld.com/b.wakem/myemail.png
From: Gunnar Hjalmarsson on 27 Nov 2005 05:57 stevob(a)gmail.com wrote: > I was wondering if it is possible in perl to get a number to have > exactly two decimal places (like xx.xx). I read about Number::Format, > but that is not available for me to use. I also know you can do a > printf to display it, but I need it to be like that in the variable. perldoc -f sprintf -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
From: J?rgen Exner on 27 Nov 2005 11:25 stevob(a)gmail.com wrote: > I was wondering if it is possible in perl to get a number to have > exactly two decimal places (like xx.xx). I also know you can do a > printf to display it, but I need it to be like that in the variable. > i.e. if someone enters in 12.5, i need my variable that stores it to > store 12.50 not 12.5. Same thing if it was 12, I would need it to be > 12.00 stored. Is that possible? From a mathematical point of view 12 and 12.00 are identical. Therefore any computation using either one should get you the same result. Given the well-known limitations of floating point numbers I cannot see why you would prefer 12.00 over 12. I can think of only one situation where trailing decimals are important: experimental measurements. A results of e.g. 12m is measured to be accurate to the meter, i.e. the actual lenght is between 11.5m and 12.5m, while 12.00m is measured to be accurate to the centimeter, i.e. the actual length is between 11995 and 12005 millimeters. But that can't be the case here because from what you wrote your Perl program would falsely increase accuracy to two decimals. May I ask _WHY_ you think you need to store numbers with two trailing decimals? Usually this idea comes from a very misguided attempt to deal with money/currency/payments? > I read about Number::Format, I may be wrong but from what I read this is about formatting numbers for output (just like printf/sprintf do) , not about the interal representation of a variable. However, if you absolutely insist on two decimals always then you could store the number as a string. Perl doesn't care if a scalar is used as a string or number. Only drawback: you would have to re-implement all arithmetic operations yourself, too. > but that is not available for me to use. > Why not? jue
From: Eric J. Roode on 27 Nov 2005 20:25 stevob(a)gmail.com wrote in news:1133088188.624275.47570 @z14g2000cwz.googlegroups.com: > Hi all, > > I was wondering if it is possible in perl to get a number to have > exactly two decimal places (like xx.xx). I read about Number::Format, > but that is not available for me to use. I also know you can do a > printf to display it, but I need it to be like that in the variable. > i.e. if someone enters in 12.5, i need my variable that stores it to > store 12.50 not 12.5. Same thing if it was 12, I would need it to be > 12.00 stored. Is that possible? Thanks. Actually, if someone enters 12.5, then it gets stored in perl, or any programming language as something like 00001100000000000000000000000000. So it's hard to figure out what you're asking, without your providing some more details about what you're getting at. -- Eric `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=( $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++; $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++ ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
|
Next
|
Last
Pages: 1 2 Prev: How to Remove Space in text file Next: finding maximum element in an array recursively |