sprintf formating

S

stig

Hello,
i need some help to format a value with sprintf.
lets say i have the value: 1234567.8

i would with, help of sprintf, like it too have a space every third digit,
change the dot to a comma and round the decimals to 2 digits.

i have come up with sprintf("%.2f", 1234567.8) which gives me: 1234567.80

after that i have had no luck to make it look like: 1 234 567,80
how should i write the sprintf format to acheive the above format?

thanks
stig
 
B

Brian Wakem

stig said:
Hello,
i need some help to format a value with sprintf.
lets say i have the value: 1234567.8

i would with, help of sprintf, like it too have a space every third digit,
change the dot to a comma and round the decimals to 2 digits.

i have come up with sprintf("%.2f", 1234567.8) which gives me: 1234567.80

after that i have had no luck to make it look like: 1 234 567,80
how should i write the sprintf format to acheive the above format?


I'm not 100% up to speed on sprintf, but I don't think you can do that with
just sprintf.

This will work though:


#!/usr/bin/perl

use strict;

my $val = 1234567.8;

$val = sprintf("%.2f", $val);
1 while $val =~ s/(.*\d)(\d\d\d)/$1 $2/;
$val =~ s/\./,/;

print "$val\n";
 
T

Tad McClellan

stig said:
i would with, help of sprintf, like it too have a space every third digit,
change the dot to a comma and round the decimals to 2 digits.


sprintf() can help with the rounding part.

tr/// can help with transliterating characters.

The Perl FAQ shows how to insert a comma every 3 digits:

perldoc -q numbers

How can I output my numbers with commas added?

i have come up with sprintf("%.2f", 1234567.8) which gives me: 1234567.80


So you have the rounding handled. Good.

after that i have had no luck to make it look like: 1 234 567,80
how should i write the sprintf format to acheive the above format?


Use the FAQ solution to insert commas, then change the characters
that you want changed with:

tr/,./ ,/; # comma->space and dot->comma
 
B

Brian McCauley

stig said:
i have come up with sprintf("%.2f", 1234567.8) which gives me: 1234567.80

after that i have had no luck to make it look like: 1 234 567,80
how should i write the sprintf format to acheive the above format?

Sorry, sprintf can't do that.

perldoc -q commas

Note: I've always felt the wording of this FAQ is none too helpful for
people in countries where the natural character used to delmit thousands
is not a comma.
 
L

Lars Kellogg-Stedman

Readers are cautioned to always question and test methods
presented in Perl FAQ documents.

Hey Purl Gurl,

We're all still waiting for some sample code demonstrating a subprocess
affecting the cwd of the caller.

-- Lars
 
T

Tad McClellan

Purl Gurl said:
Readers are also cautioned many old Perl FAQs are written by
inexperienced Perl programmers, reflecting beginner level methods.


Readers are also cautioned many new posts are written by
inexperienced Perl programmers, reflecting beginner level thinking.
 
M

Mark Clements

Purl said:
My method is about three-hundred percent more efficient
than the "sub commify" Perl FAQ method.

Readers are cautioned to always question and test methods
presented in Perl FAQ documents. Some are very good methods,
some are simply not worth using. All methods should be gauged
as to suitability for a specific given task; a previously bad method
might be the best method for a different task.

Readers are also cautioned many old Perl FAQs are written by
inexperienced Perl programmers, reflecting beginner level methods.
Simply because a method is in a Perl FAQ does not mean it is
a good method; don't be a mindless minion.

Benchmark: timing 100000 iterations of PerlFAQ, PurlGurl...
PerlFAQ: 10 wallclock secs (10.16 usr + 0.00 sys = 10.16 CPU) @ 9842.52/s
PurlGurl: 2 wallclock secs ( 3.19 usr + 0.00 sys = 3.19 CPU) @ 31347.96/s

Again, you labour under the misapprehension that fastest execution is
the primary consideration when choosing an appropriate solution. Most
programmers learn sooner or later that it comes rather far down the
list. Do you also choose your car according to which one goes fastest?

Mark
 

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

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top