Suppress conversion to scientific notation

  • Follow


gawk 3.1.0, Solaris 8

When I multiply numbers, gawk stores the answer in scientific notation 
(1.234e+06). Is there a way to suppress this from happening and store 
the answer as a string?

I tried using sprintf() but didnt quite get what I was looking for

For example:

gawk 'BEGIN {x=sprintf("%s",0.0106537*(10^8));printf("%s\n",x)}' /dev/null
1065370

This is fine, but when I do

gawk 'BEGIN {x=sprintf("%s",00.0106537*(10^8));printf("%s\n",x)}' /dev/null
0

Why do I get 0 above? I even tried converting it to a number before 
multiplying

gawk 'BEGIN {x=sprintf("%s",(00.0106537+0)*(10^8));printf("%s\n",x)}' 
/dev/null

Still 0

Help? Thanks


0
Reply Vikas 9/21/2004 3:41:51 PM

In article <2rb0e1F17o2rlU1@uni-berlin.de>,
 Vikas Agnihotri <usenet@vikas.mailshell.com> wrote:

> gawk 3.1.0, Solaris 8
> 
> When I multiply numbers, gawk stores the answer in scientific notation 
> (1.234e+06). Is there a way to suppress this from happening and store 
> the answer as a string?

No, it stores it as a floating point number.  It just prints it by 
default using scientific notation.

CONVFMT is defaulted to "%.6g"

If you want an integer, then try using x = int(x)

If you want fixed point notation, then try changing CONVFMT to "%.6f" or 
whatever you desire, or just print it explicitly with the desired %f

        printf "%10.2f\n", x

If you want to create a string for some reason then

        str = sprintf "%10.2f", x

> I tried using sprintf() but didnt quite get what I was looking for
> 
> For example:
> 
> gawk 'BEGIN {x=sprintf("%s",0.0106537*(10^8));printf("%s\n",x)}' /dev/null
> 1065370
> 
> This is fine, but when I do
> 
> gawk 'BEGIN {x=sprintf("%s",00.0106537*(10^8));printf("%s\n",x)}' /dev/null
> 0
> 
> Why do I get 0 above? I even tried converting it to a number before 
> multiplying
> 
> gawk 'BEGIN {x=sprintf("%s",(00.0106537+0)*(10^8));printf("%s\n",x)}' 
> /dev/null
> 
> Still 0
> 
> Help? Thanks

I tried your math and I got zero too, even when I substituted %f 
formatting notation.  Then I changed the order of your arguments and I 
started getting useful information 

_BUT_ when I removed that double zero at the front of the 00.0106537, it 
started working.  Try it.  Also try %f or int(x) if thay are what you 
desire.

                                        Bob Harris
0
Reply Bob 9/22/2004 12:06:34 AM


On Tue, 21 Sep 2004, Vikas Agnihotri wrote:

> gawk 3.1.0, Solaris 8
> 
> When I multiply numbers, gawk stores the answer in scientific notation 
> (1.234e+06). Is there a way to suppress this from happening and store 
> the answer as a string?
> I tried using sprintf() but didnt quite get what I was looking for
> 
> For example: 
> gawk 'BEGIN {x=sprintf("%s",0.0106537*(10^8));printf("%s\n",x)}' /dev/null
> 1065370
> 
> This is fine, but when I do 
> gawk 'BEGIN {x=sprintf("%s",00.0106537*(10^8));printf("%s\n",x)}' /dev/null
> 0
> Why do I get 0 above?

I tried 3 different awks on Solaris 9.
/opt/sfw/bin/gawk and nawk both produced 1065370
/opt/csw/bin/gawk got the 0

/opt/sfw/bin/gawk --version
GNU Awk 3.0.6
/opt/csw/bin/gawk --version
GNU Awk 3.1.4


0
Reply Gerry 9/22/2004 2:42:22 PM

In article <Pine.GSO.4.21.0409220737140.5731-100000@earth>,
Gerry  <gerryt2@vcn.bc.ca> wrote:
>On Tue, 21 Sep 2004, Vikas Agnihotri wrote:
>
>> gawk 3.1.0, Solaris 8
>> 
>> When I multiply numbers, gawk stores the answer in scientific notation 
>> (1.234e+06). Is there a way to suppress this from happening and store 
>> the answer as a string?
>> I tried using sprintf() but didnt quite get what I was looking for
>> 
>> For example: 
>> gawk 'BEGIN {x=sprintf("%s",0.0106537*(10^8));printf("%s\n",x)}' /dev/null
>> 1065370
>> 
>> This is fine, but when I do 
>> gawk 'BEGIN {x=sprintf("%s",00.0106537*(10^8));printf("%s\n",x)}' /dev/null
>> 0
>> Why do I get 0 above?
>
>I tried 3 different awks on Solaris 9.
>/opt/sfw/bin/gawk and nawk both produced 1065370
>/opt/csw/bin/gawk got the 0
>
>/opt/sfw/bin/gawk --version
>GNU Awk 3.0.6
>/opt/csw/bin/gawk --version
>GNU Awk 3.1.4

Current gawk thinks that numbers starting with a leading 0 are octal. If you
use --traditional or --posix the behavior should revert to normal. I will
try to figure out how to make gawk smarter about something like this. In any
case, trimming the double 00 down to plain 0 will fix the problem too.
-- 
Aharon (Arnold) Robbins --- Pioneer Consulting Ltd.	arnold AT skeeve DOT com
P.O. Box 354		Home Phone: +972  8 979-0381	Fax: +1 206 350 8765
Nof Ayalon		Cell Phone: +972 50  729-7545
D.N. Shimshon 99785	ISRAEL
0
Reply arnold 9/22/2004 3:04:11 PM

3 Replies
1137 Views

(page loaded in 0.022 seconds)

Similiar Articles:













7/19/2012 3:09:44 PM


Reply: