Formatting - decimal value

Hi all,

I need some input concerning formatting a decimal value. I'd like to format a decimal as follows:
Decimal length = 8, precision = 2, signed (+ for postive, - for negative)

Examples

Input                             Output
0                                    +000000.00
-23.435456                   -000023.44
87654765465.234        +999999.99

If somebody can give me a direction.....would be great.

Thanks

Noobie1995

Try using print command with the right format.

$ echo $a
-000023.44
$ printf "%2.8f" $a
-23.44000000

cheers,
Devaraj Takhellambam

1 Like

Add plus to Devaraj's solution to get plus sign before positive numbers

$ a=9
$ printf "%+2.8f" $a
+9.00000000
$ a=-9.999
$ printf "%+2.8f" $a
-9.99900000
1 Like

Hi,

looks like an idea but I have a problem by adding zero digits:

-23.435456 --> Ouptut: -000023.44

Thanks.

BTW: I had a typo: Decimal length = 2, precision = 8

Add zero

$ printf "%+010.2f" "-23.435456"
-000023.44
1 Like

The posted code from anbu23 works and is okay for my intention!