|
|
Up |
|
|
  |
Author: jidannijidanni Date: Mar 30, 2008 13:09
Why is there no way to tell printf to zero pad like the right column:
0.1 :0.100
0.05 :0.050
0.03 :0.030
0.025 :0.025
0.02 :0.020
0.015 :0.015
0.0125 :0.0125
0.01 :0.010
0.009 :0.009
0.00625:0.00625
0.005 :0.005
The challenge: Change only the "WHAT?" below to produce the right
column above. Thanks.
use constant S => 100000;
for ( 10000, 5000, 3000, 2500, 2000, 1500, 1250, 1000, 900, 625, 500 ) {
printf "%%-7g:WHAT?\n", $_ / S, $_ / S;
}
|
| |
|
| | 12 Comments |
|
  |
Author: John W. KrahnJohn W. Krahn Date: Mar 30, 2008 22:24
> Why is there no way to tell printf to zero pad like the right column:
> 0.1 :0.100
> 0.05 :0.050
> 0.03 :0.030
> 0.025 :0.025
> 0.02 :0.020
> 0.015 :0.015
> 0.0125 :0.0125
> 0.01 :0.010
> 0.009 :0.009
> 0.00625:0.00625
> 0.005 :0.005
> The challenge: Change only the "WHAT?" below to produce the right
> column above. Thanks.
> use constant S => 100000;
> for ( 10000, 5000, 3000, 2500, 2000, 1500, 1250, 1000, 900, 625, 500 ) {
> printf "%%-7g:WHAT?\n", $_ / S, $_ / S;
> }
|
| Show full article (1.12Kb) |
|
| | no comments |
|
  |
Author: szrszr Date: Mar 31, 2008 01:23
> Why is there no way to tell printf to zero pad like the right column:
> 0.1 :0.100
> 0.05 :0.050
> 0.03 :0.030
> 0.025 :0.025
> 0.02 :0.020
> 0.015 :0.015
> 0.0125 :0.0125
> 0.01 :0.010
> 0.009 :0.009
> 0.00625:0.00625
> 0.005 :0.005
> The challenge: Change only the "WHAT?" below to produce the right
> column above. Thanks.
> use constant S => 100000;
> for ( 10000, 5000, 3000, 2500, 2000, 1500, 1250, 1000, 900, 625, 500
> ) { printf "%%-7g:WHAT?\n", $_ / S, $_ / S;
> }
|
| Show full article (0.97Kb) |
| no comments |
|
  |
Author: Paul LalliPaul Lalli Date: Mar 31, 2008 10:37
> Why is there no way to tell printf to zero pad like the right column:
Why do you assume that because you don't know the way, there is no
way?
|
| Show full article (0.70Kb) |
| no comments |
|
  |
Author: szrszr Date: Mar 31, 2008 11:05
Paul Lalli wrote:
>> Why is there no way to tell printf to zero pad like the right column:
>
> Why do you assume that because you don't know the way, there is no
> way?
>
>> 0.1 :0.100
>> 0.05 :0.050
>> 0.03 :0.030
>> 0.025 :0.025
>> 0.02 :0.020
>> 0.015 :0.015
>> 0.0125 :0.0125
>> 0.01 :0.010
>> 0.009 :0.009
>> 0.00625:0.00625
>> 0.005 :0.005
>> The challenge: Change only the "WHAT?" below to produce the right
>> column above. Thanks. ...
|
| Show full article (0.92Kb) |
| no comments |
|
  |
Author: Frank SeitzFrank Seitz Date: Mar 31, 2008 11:12
szr wrote:
> Paul Lalli wrote:
>>
>>%%.03f
>>
>>$ perl -e'printf("%%.03f\n", .1)'
>>0.100
>>
>>Paul Lalli
>
> Actually that truncates to 3 decimal places, which isn't what the op
s/truncates/rounds/;
> required:
>
> $ perl -e'printf("%%.03f\n", .00625)'
> 0.006
The 0 in .03 is useless.
|
| Show full article (0.47Kb) |
| no comments |
|
  |
Date: Mar 31, 2008 12:22
> Why is there no way to tell printf to zero pad like the right column:
> 0.1 :0.100
> 0.05 :0.050
> 0.03 :0.030
> 0.025 :0.025
> 0.02 :0.020
> 0.015 :0.015
> 0.0125 :0.0125
> 0.01 :0.010
> 0.009 :0.009
> 0.00625:0.00625
> 0.005 :0.005
|
| Show full article (0.60Kb) |
| no comments |
|
  |
Author: Frank SeitzFrank Seitz Date: Mar 31, 2008 12:57
Dr.Ruud wrote:
>
> $ perl -wle'
> print "".reverse sprintf "%%05.1f", "".reverse sprintf "%%f", $_
> for qw/.1 .05 .03 .025 .02 .015 .0125 .01 .009 .00625 .005
> 1.987654321E1/
> '
> 0.100
> 0.050
> 0.030
> 0.025
> 0.020
> 0.015
> 0.0125
> 0.010
> 0.009
> 0.00625
> 0.005
> 9.876543
> ...
|
| Show full article (0.54Kb) |
| no comments |
|
  |
Author: xhosterxhoster Date: Mar 31, 2008 13:10
> Why is there no way to tell printf to zero pad like the right column:
One reason is that what you want is ill-defined. If we are going to tweak
sprintf to make it suit our personal preferences, I'd rather see a
conversion character that behaved just like %%f if given a good number, but
returned the empty string if given either an empty string or undef (rather
than converting it to zero and then applying %%f to the zero.)
> 0.1 :0.100
> 0.05 :0.050
> 0.03 :0.030
> 0.025 :0.025
> 0.02 :0.020
> 0.015 :0.015
Apparently you want to preserve non-zero digits even if that means going
beyond 3 digits right of the decimal. But why did you stop at 4?
0.014999999999999999444888
> 0.0125 :0.0125
0.0125000000000000006938893
|
| Show full article (1.20Kb) |
| no comments |
|
  |
|
|
  |
Author: Ben MorrowBen Morrow Date: Mar 31, 2008 13:55
>> Why is there no way to tell printf to zero pad like the right column:
>
> One reason is that what you want is ill-defined. If we are going to tweak
> sprintf to make it suit our personal preferences, I'd rather see a
> conversion character that behaved just like %%f if given a good number, but
> returned the empty string if given either an empty string or undef (rather
> than converting it to zero and then applying %%f to the zero.)
>
>> 0.1 :0.100
>> 0.05 :0.050
>> 0.03 :0.030
>> 0.025 :0.025
>> 0.02 :0.020
>> 0.015 :0.015
>
> Apparently you want to preserve non-zero digits even if that means going
> beyond 3 digits right of the decimal. But why did you stop at 4?
> ...
|
| Show full article (1.47Kb) |
| no comments |
|
|
|
|
|
|