Web Server forum
Back To The Forum Home!Search!Private Messaging System

This is Interesting: Free IT Magazines Now Free shipping to   
Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > bc and fload number less than zero




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    bc and fload number less than zero  
Flyzone


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-02-07 06:31 PM

Hi, a simple question: how to print 0.555 instead of .555 with bc? (i
need the 0 to be printed)
I searched on man and old threads without success.
Thanks in advance






[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Glenn Jackman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-02-07 06:31 PM

At 2007-05-02 10:14AM, "Flyzone" wrote:
>  Hi, a simple question: how to print 0.555 instead of .555 with bc? (i
>  need the 0 to be printed)
>  I searched on man and old threads without success.

Try specifying the scale:
$ echo 'scale=3;555/1000'|bc
0.555

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry





[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Patrick


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-02-07 06:31 PM

In news:1178115291.475667.142100@p77g2000hsh.googlegroups.com,
Flyzone <flyzone@technologist.com> wrote:

> Hi, a simple question: how to print 0.555 instead of .555 with bc? (i
> need the 0 to be printed)

A simple answer: one can't, because bc doesn't print other than scale and
significant figures. A leading 0 is not a significant figures. You can
outsmart it though:

printf "0%s" $(echo "scale=3;1.11/2" | bc)






[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Patrick


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-02-07 06:31 PM

In news:slrnf3hbnm.i9s.glennj@smeagol.ncf.ca,
Glenn Jackman <glennj@ncf.ca> wrote:

> Try specifying the scale:
>     $ echo 'scale=3;555/1000'|bc
>     0.555

What version are you using?

$  bc -v
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
$  echo 'scale=3;555/1000'|bc
.555






[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Kenan Kalajdzic


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-02-07 06:31 PM

Flyzone <flyzone@technologist.com> wrote:
> Hi, a simple question: how to print 0.555 instead of .555 with bc? (i
> need the 0 to be printed)

I am not sure there is a "native" way to do this with bc.  You can,
however, define a function to prepend a zero to a number which is
between 0 and 1 (note that negative numbers are treated accordingly):

define pr(x) {
if ( x > 0 && x < 1 ) "0"
if ( x < 0 && x > -1 ) {
"-0"
x = -x
}
return x
}

After defining the function, you simply call it with the appropriate
argument (expression):

pr(10.12)
pr(.10923)

a = 10.123
b = 9.789

pr(a-b)
pr(b-a)

--
Kenan Kalajdzic





[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Glenn Jackman


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-02-07 06:31 PM

At 2007-05-02 11:43AM, "Patrick" wrote:
>  In news:slrnf3hbnm.i9s.glennj@smeagol.ncf.ca,
>  Glenn Jackman <glennj@ncf.ca> wrote:
> 
>
>  What version are you using?
>  $  bc -v
>  bc 1.06
>  Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.

An old solaris version.

$ bc -v
bc: illegal option -- v
usage: bc [ -c ] [ -l ] [ file ... ]
$ what `which bc`
/usr/bin/bc:
SunOS 5.8 Generic February 2000

>  $  echo 'scale=3;555/1000'|bc
>  .555
>


--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry





[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Flyzone


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-03-07 12:20 PM

On 2 Mag, 17:37, "Patrick" <ptri.c...@statrerv.corn> wrote:
> A simple answer: one can't, because bc doesn't print other than scale and
> significant figures.

ouc! :-P

> printf "0%s" $(echo "scale=3;1.11/2" |bc)
You gave me the idea 
Better with
printf "%.3f" $(echo "555/777"|bc -l)
It's more portable cause with the bc printing 0 like the old version
of Glenn Jackman (that i have also on some SunOs too), will print
double 0.

Thanks






[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Geoff Clare


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-04-07 06:20 PM

Flyzone wrote:
 
> You gave me the idea 
> Better with
> printf "%.3f" $(echo "555/777"|bc -l)
> It's more portable cause with the bc printing 0 like the old version
> of Glenn Jackman (that i have also on some SunOs too), will print
> double 0.

It may not be as portable as you think.  POSIX doesn't require the
printf utility to support floating-point conversions.

--
Geoff Clare <netnews@gclare.org.uk>





[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Ed Morton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-04-07 06:20 PM

Geoff Clare wrote:
> Flyzone wrote:
>
> 
>
>
> It may not be as portable as you think.  POSIX doesn't require the
> printf utility to support floating-point conversions.
>

You could always fall back on awk:

awk 'BEGIN{printf "%.3f\n",555/777}'

or, if you NEED bc for whetever reason:

echo "555/777"|bc -l | awk '{printf "%.3f\n",$0}'

Ed.





[ Post a follow-up to this message ]



    Re: bc and fload number less than zero  
Yves Pouplard


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-04-07 12:21 PM

On Wed, 2 May 2007 08:37:28 -0700, "Patrick" <ptri.c.k.@statrerv.corn>
wrote:

>In news:1178115291.475667.142100@p77g2000hsh.googlegroups.com,
>Flyzone <flyzone@technologist.com> wrote:
> 
>
>A simple answer: one can't, because bc doesn't print other than scale and
>significant figures. A leading 0 is not a significant figures. You can
>outsmart it though:
>
>printf "0%s" $(echo "scale=3;1.11/2" | bc)

printf "0%s\n" $(echo 'scale=3;555/1000'|bc) sounds better... as no
output is done without \n 

or better: printf "%0.3f" $(echo 'scale=3;555/1000'|bc) ... might try
"%0.4f", either...





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 03:51 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 

Back To The Top
Home | Usercp | Faq | Register