Perl - Get Two Decimals

S

stevob

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
 
B

Brian Wakem

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;
 
G

Gunnar Hjalmarsson

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
 
J

Jürgen Exner

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
 
E

Eric J. Roode

(e-mail address removed) wrote in @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
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
B

brian d foy

Jürgen Exner said:
(e-mail address removed) wrote:
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.

Perhaps he doesn't want the number for a computation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top