Convert floating point to a number

Hello Guys,
I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value

#!/bin/bash
# Welcome to the Interactive Shell Tutorial.
# Start by choosing a chapter and
# write your code in this window.
FLOAT=1.14475E+15
#INT=${FLOAT/.*}
#INT=${FLOAT/\.*}
S = INT(1.14475E+15)
echo $S

Thanks

Okay, you've showed us what you don't want, but I can't tell what you do want. What would a "good" result look like?

Shell does not understand floating point numbers. You'll have to pipe it into bc or awk or some other program/tool/language which does.

There are a lot of syntax errors in your code, also. You can't do X = value you have to do X=value Also there is no INT( ) function in bash. Besides which, functions don't even work that way in bash.

Thank you for the reply. I want to know what is the total number like 1,144,750,000,000,000. I do not know what the right value.. but some thing like that.

$ echo "1.14475E+15" | awk '{ V=$1+0 ; printf("%.0f\n", V); }'
1144750000000000

$ V=$( echo "1.14475E+15" | awk '{ V=$1+0 ; printf("%.0f\n", V); }' )

$ echo $V
1144750000000000

$
awk 'BEGIN {printf "%.f\n", "1.14475E+15"}'
1144750000000000
1 Like

There is also a printf command

FLOAT=1.14475E+15
INT=$(printf "%.f" "$FLOAT")
echo $INT
1 Like

Hi.

If you have a utility such as some of the results for a Google search like commify comma insertion linux OR unix you could use:

$ commify $(printf "%.f" "$FLOAT")
1,144,750,000,000,000

With a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.9 (jessie) 
bash GNU bash 4.3.30
commify - ( local: RepRev 1.3, ~/bin/commify, 2014-10-09 )

Best wishes ... cheers, drl

It's fun to make an own commify.

printf "%.f\n" "$FLOAT" |
sed '
  /\./!s/$/./
  :Loop
  s/\([0-9]\)\([0-9]\{3\}[.,]\)/\1,\2/
  t Loop
  s/[.]$//
'

Or with good locale:

$ LC_NUMERIC="C" /usr/bin/printf "%'.f\n" "1.14475E+15"
1144750000000000
$ LC_NUMERIC="POSIX" /usr/bin/printf "%'.f\n" "1.14475E+15"
1144750000000000
$ LC_NUMERIC="fr_FR.utf8" /usr/bin/printf "%'.f\n" "1.14475E+15"
1 144 750 000 000 000
$ LC_NUMERIC="en_AG.utf8" /usr/bin/printf "%'.f\n" "1.14475E+15"
1,144,750,000,000,000

PS: Don't believe ' in format string %'.f\n .

Regards.

3 Likes

Thank you every one. It worked