Format large numbers with commas at every thousandth decimal spot
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Format large numbers with commas at every thousandth decimal spot
Hi,
I wrote a bash script that logs some very large (and occasional very small) numbers. I would like to have commas at every thousandth spot to make it easier to read. I assume there must be some program or built-in command (in bash) that does this, but I can't find it.
Can someone please let me know what the command is? If there is no command, is there a simple way to do this?
Just to make sure everyone understands what I am looking for:
Let's say I have the following number: 45678923
printf is an external program, not a builtin in most shells. I'm not sure about all shells, but it's definitely the case in ksh and bash. With this in mind, you will find that executing printf a lot of times in a tight loop will lead to terrible performance, since a new process must be created each time it is called.
In my experience, when you start to hit this level of complexity, it's usually worth switching to using something like perl or python for your program.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by nwchow
I tried in ksh. It does not recognize the single quote after the % sign. It said "invalid directive"
Try using recent a ksh release which supports this syntax with its printf builtin or the external printf command (/bin/printf).
I'm using ksh93s+ and it works.
Code:
$ print ${.sh.version}
Version M 1993-12-28 s+
$ type printf
printf is a shell builtin
$ LC_ALL=en_US printf "%'d\n" 12345678
12,345,678
As a side note. This numerical notation is awful to me, as the comma is the standard decimal separator in the French and probably some other European locales.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.