printf formatting


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers printf formatting
# 1  
Old 03-02-2010
printf formatting

Is there a way to make these 2 numbers - $482477.37 and $1875000.00

look like $482,477.37 and $1,875,000.00 with printf?
# 2  
Old 03-02-2010
This is depending on the setting of your locale. Try this within the shell (script):
Code:
printf "$%'.2f\n" 482477.37

# 3  
Old 03-03-2010
We can achieve in some other way like sed , perl , bash but there is no way to do this with the printf command.
# 4  
Old 03-03-2010
Bug

We can do it in php also using number_format() function.

Sample code:

Code:
<?php
$number = 482477.37;
$value = number_format($number,2);
echo "\$$value";
?>

# 5  
Old 03-03-2010
MySQL

There is no way to do this with the printf command.
See the following Example. Formate the numbers using SED

The Formate file contents is bellow.

file-name "Formate."
------------------------------------------
:loop
s/^([0-9]+)([0-9]{3}.*)/\1,\2/
t loop

------------------------------------------

command:
Code:
 sed -r -f Formate

Output:
482477.37
482,477.37
1875000.00
1,875,000.00


you can use the sed command

Last edited by ungalnanban; 03-09-2010 at 01:02 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Passing printf formatting parameters as variables

Hi, I haven't programed in C in a few years. I have been doing a lot of shell scripting, I.E. not really programming anything heavy. :o That said, I have a script that gives hourly usage statistics for our email server. It runs w-a-y to slow as a script for my impatience, and needs to... (7 Replies)
Discussion started by: mph
7 Replies

2. Shell Programming and Scripting

Text Formatting using printf[ksh]

Hi All, I am attempting to create a fixed length tilde delimited file using printf. The variables are initialized to fixed length blank spaces a=' ' b=' ' c=' ' d=' ' Sometimes the variable might contain values and sometimes they are... (5 Replies)
Discussion started by: angie1234
5 Replies

3. Shell Programming and Scripting

Printf command

Hi, I a sequance number from 1-999 and i want asing the value like 001,002..999 Exp: file_001 file_002 file_003... file_999 How can i disaplay the sequnace number as mention above. (3 Replies)
Discussion started by: koti_rama
3 Replies

4. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

5. Shell Programming and Scripting

Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file: ID X 1 7 1 8 1 3 2 5 2 7 2 2 To something like this: ID X1 X2 X3 1 7 8 3 2 5 7 2 I've tried the following loop: for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
Discussion started by: flotsam
4 Replies

6. UNIX for Dummies Questions & Answers

Need help with printf

Hi, I have just completed my first script (:D) and now i just need to format it with printf. This is what I have: #!/bin/ksh TOTB=0 TOTF=0 TOTI=0 HOST=`hostname` echo " FSYSTEM BLKS FREE INUSE MOUNTEDON" df -m | grep -v ":"|grep -v Free|grep -v "/proc"| while read FSYSTEM... (2 Replies)
Discussion started by: compan023
2 Replies

7. Shell Programming and Scripting

IF and awk/printf

Hi Friends, Scripting newb here. So I'm trying to create a geektool script that uses awk and printf to output certain fields from top (namely command, cpu%, rsize, pid and time, in that order). After much trial and error, I've pretty much succeeded, with one exception. Any process whose name... (3 Replies)
Discussion started by: thom.mattson
3 Replies

8. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

9. Shell Programming and Scripting

printf

How to print output in following format? A..................ok AA................ok AAA..............ok AAAAAA........ok "ok" one under one (4 Replies)
Discussion started by: mirusnet
4 Replies

10. Programming

printf

What is the output of the following program considering an x86 based parameter passing sequence where stack grows towards lower memory addresses and that arguments are evaluated from right to left: int i=10; int f1() { static int i = 15; printf("f1:%d ", i); return i--; } main() {... (2 Replies)
Discussion started by: arunviswanath
2 Replies
Login or Register to Ask a Question