0

My script is:

#!/bin/bash
num1=32.55
num2=145.958
printf "%5.2f\n" $num1
printf "%5.2f\n" $num2

The output is:

32.55
145.96

Instead of:

   32.55
  145.96

What's going on? If it's any help, I'm using Fedora 27 Beta with Linux Kernel 4.13.8-300.fc27.x86_64

1 Answer 1

6

The width specifier is for all characters including the decimal point and fraction. Your numbers already have at least 5 characters, so...

$ printf "%8.2f\n" 32.55
   32.55
2
  • So basically it's %(total).(num after decimal)f ? Commented Oct 25, 2017 at 8:12
  • For f and similar, yes. Commented Oct 25, 2017 at 8:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .