LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Printing a number in standard thousand format using printf (https://www.linuxquestions.org/questions/linux-newbie-8/printing-a-number-in-standard-thousand-format-using-printf-4175609278/)

MahendraL 07-06-2017 01:43 AM

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.

grail 07-06-2017 02:40 AM

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.

aragorn2101 07-06-2017 02:50 AM

Check if the LC_ALL variable is set on both machines. Check this out: https://wiki.archlinux.org/index.php/locale

MahendraL 07-06-2017 10:58 PM

Quote:

Originally Posted by grail (Post 5731299)
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.

grail 07-07-2017 02:05 AM

Gald you found your solution :) Please remember to mark the question as SOLVED.


All times are GMT -5. The time now is 05:57 AM.