Skip to main content

Command Palette

Search for a command to run...

Bash Script That Prints A Number With Two Decimal Places

Updated
4 min read
Bash Script That Prints A Number With Two Decimal Places
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

Another task that I had to tackle as part of the lessons on shell expansion in my ongoing ALX Software Engineering course was to write a bash script that prints a number with two decimal places.

Let's take a practical example and solve it to explain how to print a number with two decimal places.

Practical Example:

Write a script that prints a number with two decimal places, followed by a new line.
The number will be stored in the environment variable MYNUM

Up until this point, we had always relied on the echo command to display or print somethings to the screen. As such, upon reading this question, my intuition defaulted to using the echo command to perform the task.

Unfortunately, I learned too soon that the echo command prints out only whole numbers and doesn't print floats by default. This means that we need to rely on something else to help us print floats or decimal numbers to the screen.

The printf command is the saviour of the situation. This is a popular command in programming languages like C but the bash shell equally utilizes it for the same purpose.

Printf is able to let us determine the format of the output we want. Hence, in this case the printf command will help us to format the output to be in 2 decimal places.

How to format output with the printf command

Let's look at the normal synthax of the printf command.

printf options arguments

It may also be presented as

printf options [format specifiers] arguments

The format specifiers are what tells the printf command what the format of the output should be. They also point to the arguments and restricts the type or format of values the printf command takes in as arguments.

Examples of format specifiers that you are likely to use include

Format specifier Description
%d decimal (integer) number (base 10)
%f floating-point number
%i integer number (base 10)
%o octal number (base 8)
%x hexadecimal number (base 16)

Let's see the printf in action so we can better understand it.

Using printf to print unformatted text

You can pass an argument to the command without any format specifiers. For example, if I want the shell to display my full name, I can pass my name as an argument to the printf command.

printf "Ehoneah Obed"

image.png

The outcome is a bit messy. In that everything is jammed up onto a single line. To avoid such a problem, we have to introduce the newline formatter\n at the end of it.

printf "Ehoneah Obed \n"

image.png

Can you spot the difference?

Now, let's try printing out a float with the printf command.

printf "2.555 \n"
2.555

Another way to write the above using a format specifier

printf "%f \n" 2.555
2.555000

What this command does is that, the shell will have to replace the %f with whichever value you pass as an argument (2.555 in this case). So, in essence, this is the same thing as the earlier one that we wrote.

We can leverage the style for the last command above to specify the number of decimal places allowed.

To achieve that, you have to follow the % with a dot . and the number of decimal places you want to output before the f.

Example: Here is what you will get if you want to out the number 2.555 as a two decimal places float.

printf "%.2f \n" 2.555
2.56

You can therefore replace the number 2 in the command above and get your answer displayed in any given number of decimal places.

Solution: Print a number with two decimal places, followed by a new line

NB: The number will be stored in the environment variable MYNUM

This is going to be done just like the example we did above. You use the printf command with a float specifier %f but specify 2 decimal places, %.2f.

The full command with the variable MYNUM will look like this:

printf "%.2f \n" $MYNUM

Conclusion

I hope this makes things clearer and you understand everything we did in order to arrive at the answer. If you have any questions, comment it below and I will respond to it. You can also make suggestions on how I can make such tutorials better for you.

Thanks for reading and I will love to connect personally with you. If you are on Twitter then you can send me a DM

O

if only my medical school courses were explained this way maybe i wouldnt have changed career paths Thank you Dr for explaining

A

Thank you very much for this great explanation.

1
E

Clear, concise, and shared in a manner to facilitate learning. Good job, Obed.

1
T

I just wanted to say that i really appreciate you for your work. You aren't just giving off answers or solutions to these problems, you give a very intuitive description of what the problem is asking and actually build up from that while explaining how you got to the final solution. I also appreciate how you go ahead to describe each and every command you use, which really makes it easier for a beginner/learner like me understand firmly actually what is happening. I sometime find myself reading through some articles that barely explain the code written or rather use very DIFFICULT to understand technical terms which really put me off and effectively discourage me from even trying to learn the concept. I, again, really appreciate your method. I have looked through some of your other have been able to learn quite a lot from you. I am glad people like you exist.

2
T

By the way, I am also doing the ALX Software Engineering program😅. It really inspires me to see how much you already know and are able to share. I want to do the very same, i will work towards that. Thank you again Dr. Ehoneah Obed

1
D

Thank you for your kind ways and I am always happy to help. All the best

2

More from this blog

D

Dr. Ehoneah Obed

75 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.