Re: Variable division, assignment and sprintf in one line
- From: iaccounts@xxxxxxxxxx (Steve Bertrand)
- Date: Wed, 28 Nov 2007 19:48:47 -0500
$var = sprintf ("%.2f", (($var /=1024) /=1024)))
What the ...?!?
/= ?
It was an off the top of my head example of what I wanted to do. Without
being at the code at the time, I was simply thinking +=.
You divide the value of $var by 1024, assign it back to $var, divide
it by another 1024, assign it again to $var, then format the
resulting number using sprinf() and assign it to $var?!? Why???
Why? Because I am not a professional, hence why I am asking at
'beginners' ;)
$var = sprintf ("%.2f", $var / (1024*1024));
And it will be more efficient because the multiplication will be done
at compile time (once) and you'll only do a single division each
time.
I did not know that multiplication would be done at compile time. Is
this true for any mathematical equation that does not include a runtime
variable?
Which is also more precise, though in this case it doesn't seem
to matter.
'twasn't about precision. It was a theoretical 'how can I do this?'.
I preferred Gunnar's answer as it let me offload it to a different
subroutine for use by other pieces of the module, only because I have a
few other type of equations of the same sort.
Thanks for your critique :)
Steve
.
- Follow-Ups:
- Re: Variable division, assignment and sprintf in one line
- From: Jenda Krynicky
- Re: Variable division, assignment and sprintf in one line
- References:
- Variable division, assignment and sprintf in one line
- From: Steve Bertrand
- Re: Variable division, assignment and sprintf in one line
- From: Jenda Krynicky
- Variable division, assignment and sprintf in one line
- Prev by Date: Re: Extracting one record from multiple-records textfile
- Next by Date: Re: Variable division, assignment and sprintf in one line
- Previous by thread: Re: Variable division, assignment and sprintf in one line
- Next by thread: Re: Variable division, assignment and sprintf in one line
- Index(es):