help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: scientific notation in Bash


From: Tapani Tarvainen
Subject: Re: scientific notation in Bash
Date: Tue, 15 Mar 2022 14:45:22 +0200

On Tue, Mar 15, 2022 at 08:13:28AM -0400, Greg Wooledge (greg@wooledge.org) 
wrote:

> On Tue, Mar 15, 2022 at 01:03:54PM +0100, Ante Bilandzic wrote:
> > However, there seems to be the corner case, when I was expecting that Bash
> > will be able to handle the problem internally: the case when an integer is
> > written in a scientific notation, using 'e' or 'E'.
> 
> As you have already observed, bash doesn't handle that.  You would need
> to run some external utility (e.g. awk) to convert those into a form
> that bash can read.
> 
> unicorn:~$ awk -v n=4.4e5 'BEGIN {printf("%d\n", n)}'
> 440000
> unicorn:~$ awk -v n=4.4e9 'BEGIN {printf("%d\n", n)}'
> 4400000000

Bash can do it with the printf builtin:

$ printf "%.0f\n" 4.4e5
440000
$ printf "%.0f\n" 4.4e9
4400000000

So instead of Var=44e1 you could do

printf -v Var "%.0f" 44e1

-- 
Tapani Tarvainen



reply via email to

[Prev in Thread] Current Thread [Next in Thread]