How To Make Decimal Point Fall On The 15th Character On The Screen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How To Make Decimal Point Fall On The 15th Character On The Screen
# 1  
Old 12-08-2005
How To Make Decimal Point Fall On The 15th Character On The Screen

If i have a variable which is a decimal number, i.e 34.05 How can you make decimal point fall on the 15th character on the screen? Or any other that you can specify? Can you do it using sed or awk?
# 2  
Old 12-08-2005
It would help if you would say what shell you're using. With ksh...

Code:
$ x=24.15
$ typeset -R14 x1=${x%.*}
$ x="$x1${x##+([!.])}"
$ echo "123456789012345" ; echo "$x"
123456789012345
            24.15
$

# 3  
Old 12-08-2005
I am using bash shell, thanks for reply, but what i wanna do is have something like this

24. 40 (where 40 is on 15th character of the screen)
# 4  
Old 12-08-2005
hmm, you want a script that gets a number and transforms this number in a way that you get: fourteen characters (missing digits filled by leading blanks), then a decimal point, then any number of digits that would come after the comma. Is that correct?

If so:
you could format your output with printf, using the "%f" clause. The following would display the decimal point on the fifteenth column (25-10), but fills up the last 6 digits after the comma with zeroes:

Code:
printf "%25.10f\n" 1234.5678

You can: count the number of digits after the comma and use shell variables to fill into the printf format. In our example:

Code:
typeset -i after=4
typeset -i before=0

(( before = 15 + after ))

printf "%${before}.${after}f\n" 1234.5678

Hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies

2. UNIX for Beginners Questions & Answers

How to add following decimal point to a CSV value?

hi there I being trying to figure out way to add " .0" to an integer value in a csv using sed or awk with out success. just as a work around for 2147483647 32 bit limitation that influxdb is currently having the data base will accept values and work fine if it has the XXX.0 ... (7 Replies)
Discussion started by: sash99
7 Replies

3. Shell Programming and Scripting

Add Delimiter after 2 decimal point for a particular column

Hi All, Please help for the following scenario where I want to add a delimiter after 2 decimal point integer. Original Line 1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05000§762.59§00395§M§301223.0528§1165§2017-03-31 20:34:25 Expected Line... (12 Replies)
Discussion started by: ckwan123
12 Replies

4. Shell Programming and Scripting

Moving decimal point in a series of numbers

Hi, i need to move the decimal point from a file listing some numbers like this : 49899.50 49914.55 49894.48 49939.65 49879.44 49919.57 49934.62 49944.67 49954.72 (1 Reply)
Discussion started by: Board27
1 Replies

5. Shell Programming and Scripting

Help with Round Up with 2 decimal point at specific column

Input file: USA 20.5683 UK 3.54221 Japan 2.54001 China 2.50897 Germany 2.05816 . . Desired output file: USA 20.57 UK 3.54 Japan 2.54 China 2.51 Germany 2.06 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. Shell Programming and Scripting

In Sed how can I replace starting from the 7th character to the 15th character.

Hi All, Was wondering how I can do the following.... I have a String as follows "ACCTRL000005022RRWDKKEEDKDD...." This string can be in a file called tail.out or in a Variable called $VAR2 Now I have another variable called $VAR1="000004785" (9 bytes long), I need the content of... (5 Replies)
Discussion started by: mohullah
5 Replies

7. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

8. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

9. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

10. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies
Login or Register to Ask a Question