Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • Linux Shell Script Programming
  • Format very large numbers in Bash shell scripts?

Format very large numbers in Bash shell scripts?

March 18, 2019 / Dave Taylor / Linux Shell Script Programming / No Comments

I am writing a shell script that summarizes large amounts of data and would like to display the sum values with thousands separators. By default I just get a long stream of digits. What’s a smart way to solve this in a shell script?

This sounds like a classic programming problem and in the world of Linux and Bash shell scripts, there are quite a few ways to solve it. Where this gets interesting, however, is that different locales have different styles for thousands separators. For example, in the United States, 12,345.00 is the correct format, but in Canada that would be written as 12 345,00 and in Spain that would be 12.345,00. A bit confusing, but in Linux it’s stored as LANG and a variety of different LC_* variables. In this case, the two values we care about are the radix (the element between the whole and fractional portions (between 5 and 0 above) and the thousands separator, known as the thousands_sep.

linux penguinBut this is probably way more than you want to know. The key is that your easiest answer is to use the printf command line function (its name comes from the same function in the C programming language). It lets you specify as a format string what you want output, then the value or values to be included.

In this case, let’s say that num=12345678.90 to make things interesting. Use echo and, well, you’ll get just that:

$ echo $num
12345678.90

But if we use printf and specify the %f format for a floating number (e.g. one that could have a fraction), we get:

$ printf "%f\n" $num
12345678.900000

You can see that it hasn’t worked, but the printf function has at least recognized the decimal component as it uses a default of four digits of significance. To add the thousands separator you need to use a single quote in the % sequence. To chop that 4 digits after the radix or decimal point to just two, we’ll add .2 additionally. The result:

$ printf "%'.2f\n" $num
12,345,678.90

Sweet, but there’s still a bit of a problem as this invites some problems if you want this command & period format and you’re in a locale that specifies a different format that you don’t much like.

mac locale number format

So the output would be correct for your location, but how can you force the commas and period? By temporarily specifying a different locale just for the invocation of this command:

$ LC_ALL=en_US.UTF-8 printf "%'.3f\n" $num
12,345,678.90

Turns out that GNU Linux, and therefore many Linux distributions, also include a cool command called numfmt. With this utility you could simply specify numfmt –grouping $num and get the same output. You can, of course, use either of these strategies directly in a shell script as needed, either as a small function or inline in your script.

Hope that helps you out!

Pro Tip: I’ve been writing about shell script program since before Linux was a thing! Check out my shell script programming help for lots more tutorials and Q&A discussion.

About the Author: Dave Taylor has been involved with the online world since the early days of the Internet. Author of over 20 technical books, he runs the popular AskDaveTaylor.com tech help site. You can also find his gadget reviews on YouTube and chat with him on Twitter as @DaveTaylor.

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!
bash shell, locale, number format, number formats, numfmt, printf

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • How to Enlarge Font Size in Apple’s Books App on the iPad
  • Chromebook Owner’s Guide to Antivirus & Anti-Malware Solutions
  • Everything You Need to Know about Apple’s Clean Energy Charging
  • How Can I Watch Free Classic Movies on my Windows PC?
  • How Can I Maximize Online Privacy with a VPN Connection?

On Our YouTube Channel

TWT Audio REVO TW310 Budget Headset -- DEMO & REVIEW

iClever Bluetooth 34-Key Number Pad Keyboard -- REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help (164)
  • Android Help (228)
  • Apple iPad Help (148)
  • Apple Watch Help (53)
  • Articles, Tutorials, and Reviews (346)
  • Auto Tech Help (17)
  • Business Advice (200)
  • ChromeOS Help (34)
  • Computer & Internet Basics (782)
  • d) None of the Above (166)
  • Facebook Help (384)
  • Google, Chrome & Gmail Help (188)
  • HTML & Web Page Design (247)
  • Instagram Help (49)
  • iPhone & iOS Help (625)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (99)
  • LinkedIn Help (88)
  • Linux Help (174)
  • Linux Shell Script Programming (90)
  • Mac & MacOS Help (914)
  • Most Popular (16)
  • Outlook & Office 365 Help (33)
  • PayPal Help (68)
  • Pinterest Help (54)
  • Reddit Help (19)
  • SEO & Marketing (82)
  • Spam, Scams & Security (96)
  • Trade Show News & Updates (23)
  • Twitter Help (222)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (951)
  • Wordpress Help (206)
  • Writing and Publishing (72)
  • YouTube Help (47)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (62)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2023 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy