LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2017, 01:43 AM   #1
MahendraL
LQ Newbie
 
Registered: Jul 2009
Posts: 29

Rep: Reputation: 0
Printing a number in standard thousand format using printf


Hi,

Please see the C program below.

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main (int argc, char* argv[])
{
setlocale (LC_ALL, "");
printf ("%'llu\n", strtoull("109488", (char**) NULL, 10));
return 0;
}

When it's compiled and run on a Redhat 6.6 Linux machine, the output is
109,488

When it's compiled and run on a Ubuntu 14.04.3 LTS machine, the output is
1,09,488

Could someone please tell how can I get the 1st output (109,488) on the second machine (Ubuntu) ?

Thank you.
 
Old 07-06-2017, 02:40 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Please use [code][/code] tags around your code

What happens when you use the same printf at the command line?
Code:
$ printf "%'llu\n" 109488
109,488
Could be your locale settings on the Ubuntu machine.
 
1 members found this post helpful.
Old 07-06-2017, 02:50 AM   #3
aragorn2101
Member
 
Registered: Dec 2012
Location: Mauritius
Distribution: Slackware
Posts: 567

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Check if the LC_ALL variable is set on both machines. Check this out: https://wiki.archlinux.org/index.php/locale
 
1 members found this post helpful.
Old 07-06-2017, 10:58 PM   #4
MahendraL
LQ Newbie
 
Registered: Jul 2009
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Please use [code][/code] tags around your code

What happens when you use the same printf at the command line?
Code:
$ printf "%'llu\n" 109488
109,488
Could be your locale settings on the Ubuntu machine.
At command line, the printf printed 1,09,488 i.e. the same as the C program.
Somehow I found that if I add
setlocale(LC_NUMERIC, "en_US.UTF-8");
to my C program, then it prints the number in desired thousand format.
Thanks grail.
 
Old 07-07-2017, 02:05 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Gald you found your solution Please remember to mark the question as SOLVED.
 
  


Reply

Tags
printf


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C programme to printf the week number of the year? Xeratul Programming 9 05-24-2017 01:07 PM
[SOLVED] C, gcc, printf Not Printing killingthemonkey Programming 10 02-07-2017 06:21 AM
Printing of application out in standard and non standard paper size gkedar614 Linux - Newbie 2 08-23-2013 03:40 AM
printf format does not work ! casualzone Programming 3 01-04-2012 02:31 AM
Is there a currency format with printf() ? LUB997 Programming 1 01-03-2005 08:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration