Ask a Question related to PERL Beginners, Design and Development.
-
Marcos Rebelo #1
RE: help float to string
try the sprintf
print sprint("%.02f", $f);
if I'm not rong;
-----Original Message-----
From: [email]perl@swanmail.com[/email] [mailto:perl@swanmail.com]
Sent: Tuesday, October 14, 2003 10:18 AM
To: [email]beginners@perl.org[/email]
Subject: help float to string
Can someone help me with retaining the precisions when they are zero?
ie -
$f = 9.00
$z = $f;
print $z; #result in 9 - bad I want 9.00
$fx = 9.25
$z = $fx;
print $z; #result in 9.25 - good
I can but don't want to use a sub like itoa. Is there a better way to
convert to string?
$z=itoa($f);
print $z #result 9.00
$z=itoa($fx);
print $z #result 9.25
sub itoa { return sprintf("%.2f", $_[0]); }
-----------------------------------------
eMail solutions by
[url]http://www.swanmail.com[/url]
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
Marcos Rebelo Guest
-
#39126 [NEW]: String->float->String conversion behavior
From: bobson at rpg dot pl Operating system: Linux PHP version: 5CVS-2006-10-11 (snap) PHP Bug Type: Unknown/Other Function... -
#25562 [Opn->Bgs]: Float to String to Float conversion error
ID: 25562 Updated by: helly@php.net Reported By: daseymour at 3hc dot org -Status: Open +Status: ... -
#25562 [NEW]: Float to String to Float conversion error
From: daseymour at 3hc dot org Operating system: and PHP version: 4.3.3 PHP Bug Type: Math related Bug description: Float... -
How to convert string to float?
i want to covert a querystring to float number in asp. what function should i use? i know cint() can convert string to int but how about float? -
preventing string conversion to float.
It has been a long time since I touched any perl, and I am having a problem with parsing a text file. I need to pull out a string in the form... -
perl@swanmail.com #2
help float to string
Can someone help me with retaining the precisions when they are zero?
ie -
$f = 9.00
$z = $f;
print $z; #result in 9 - bad I want 9.00
$fx = 9.25
$z = $fx;
print $z; #result in 9.25 - good
I can but don't want to use a sub like itoa. Is there a better way to
convert to string?
$z=itoa($f);
print $z #result 9.00
$z=itoa($fx);
print $z #result 9.25
sub itoa { return sprintf("%.2f", $_[0]); }
-----------------------------------------
eMail solutions by
[url]http://www.swanmail.com[/url]
perl@swanmail.com Guest
-
Paul Johnson #3
Re: help float to string
[email]perl@swanmail.com[/email] said:
Why not? Seems like the perfect solution to me.> Can someone help me with retaining the precisions when they are zero?
>
> ie -
>
> $f = 9.00
> $z = $f;
> print $z; #result in 9 - bad I want 9.00
>
> $fx = 9.25
> $z = $fx;
> print $z; #result in 9.25 - good
>
> I can but don't want to use a sub like itoa.
That depends on your definition of better.> Is there a better way to
> convert to string?
$ perl -le '$# = "%.2f"; print 9'
9.00
But $# is deprecated, so don't do that.
$ perl -le '$f = "9.00"; $z = $f; print $z'
9.00
--> $z=itoa($f);
> print $z #result 9.00
> $z=itoa($fx);
> print $z #result 9.25
>
> sub itoa { return sprintf("%.2f", $_[0]); }
Paul Johnson - [email]paul@pjcj.net[/email]
[url]http://www.pjcj.net[/url]
Paul Johnson Guest



Reply With Quote

