Formatting - decimal value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting - decimal value
# 1  
Old 03-14-2013
Tools 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

Code:
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

Last edited by Scrutinizer; 03-14-2013 at 06:37 AM.. Reason: code tags
# 2  
Old 03-14-2013
Try using print command with the right format.

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

cheers,
Devaraj Takhellambam
This User Gave Thanks to devtakh For This Post:
# 3  
Old 03-14-2013
Add plus to Devaraj's solution to get plus sign before positive numbers
Code:
$ a=9
$ printf "%+2.8f" $a
+9.00000000
$ a=-9.999
$ printf "%+2.8f" $a
-9.99900000

This User Gave Thanks to anbu23 For This Post:
# 4  
Old 03-14-2013
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
# 5  
Old 03-14-2013
Add zero
Code:
$ printf "%+010.2f" "-23.435456"
-000023.44

This User Gave Thanks to anbu23 For This Post:
# 6  
Old 03-14-2013
The posted code from anbu23 works and is okay for my intention!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Shell Programming and Scripting

Decimal Padding in Decimal

Hi Experts, I have requirement to pad a decimal number that should have fixed length as 10. if number is 234.234 > 234.234000 if number is 12.4 > 12.4000000 if number is 3456.5678 > 3456.56780 from above example we can see that overall length is 10 and padding is being done right sided of... (2 Replies)
Discussion started by: looney
2 Replies

3. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

4. UNIX for Dummies Questions & Answers

Convert hexa decimal to decimal

Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it... (4 Replies)
Discussion started by: sudhakar T
4 Replies

5. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

6. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

7. Shell Programming and Scripting

Decimal format

Hello Friends, I have a file with below format... 1234,Hary,102.55 4567,Maria,250.40 78942,suzan,261.60 48965,Harun,179.32 And I want to check the last column. If the decimal value of the last column is below 50, I need that column else ignore that column. Can anyone help me out? (3 Replies)
Discussion started by: mziaullah
3 Replies

8. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

9. Shell Programming and Scripting

hex to decimal

hi all, echo "ibase=16;obase=10;11" | bc shouldn't i get 17? i am getting 11 i am trying to convert 11 (hex) to decimal stuck! JAK (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question