Rounding off

  • Follow


Suppose i have the following,

r = RANDOMU(SEED,1)

and if the result is 0.423832.


How do i round it off to 0.42 ??
0
Reply bala 4/21/2010 8:17:02 PM

bala murugan writes: 

> Suppose i have the following,
> 
> r = RANDOMU(SEED,1)
> 
> and if the result is Float(.
> 
> 
> How do i round it off to 0.42 ??

Print, Float(Long(r*100))

Cheers,

David

-- 
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
0
Reply David 4/21/2010 8:49:27 PM


David Fanning writes: 

> Print, Float(Long(r*100))

Whoops! You said "round it off".

  Print, Float(Round(r*100))

Cheers,

David



-- 
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
0
Reply David 4/21/2010 8:50:56 PM

On Apr 21, 3:50=A0pm, David Fanning <n...@dfanning.com> wrote:
> David Fanning writes:
> > Print, Float(Long(r*100))
>
> Whoops! You said "round it off".
>
> =A0 Print, Float(Round(r*100))
>
> Cheers,
>
> David
>
> --
> David Fanning, Ph.D.
> Fanning Software Consulting, Inc.
> Coyote's Guide to IDL Programming:http://www.dfanning.com/
> Sepore ma de ni thui. ("Perhaps thou speakest truth.")

perhaps this will do it?

print, round(f*100.)/100.
0
Reply Gianguido 4/21/2010 9:20:32 PM

David Fanning wrote:

>   Print, Float(Round(r*100))

Maybe he also wants to divide by 100 again, to get to 0.42.

It should also be noted, that this method, or indeed anything that
involves rounding, does not work if the number to round does not fit
into a 32bit or 64bit integer (you will get an exception and the
result will be garbage). If that happens, you are probably trying to
round at a position waaaaay past the last significant digit anyway,
so the whole rounding step should be skipped. But you have to test for
it.


chl
0
Reply Carsten 4/21/2010 9:39:01 PM

Gianguido Cianci writes: 

> perhaps this will do it?
> 
> print, round(f*100.)/100.

Oh, the hell with it. *Something* like that! ;-)

Cheers,

David

P.S. Why I try to answer a question when I have about
10 different things going on is a mystery to me!



-- 
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
0
Reply David 4/21/2010 9:48:42 PM

Carsten Lechte writes: 

> It should also be noted, that this method, or indeed anything that
> involves rounding, does not work if the number to round does not fit
> into a 32bit or 64bit integer (you will get an exception and the
> result will be garbage). If that happens, you are probably trying to
> round at a position waaaaay past the last significant digit anyway,
> so the whole rounding step should be skipped. But you have to test for
> it.

If you are looking for people with recent experience
to testify to how much trouble you can get into by
not testing data type bounds, I'm available at
my professional rate of $0.25/hour.

Cheers,

David 



-- 
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
0
Reply David 4/21/2010 9:52:12 PM

bala murugan wrote:
> Suppose i have the following,
> 
> r = RANDOMU(SEED,1)
> 
> and if the result is 0.423832.
> 
> 
> How do i round it off to 0.42 ??

IDL> r = RANDOMU(SEED,1)
IDL> print, r
       0.447645
IDL> print, r, format='(f4.2)'
       0.45

I presume you meant "only print two digits after the decimal point" when you said "round
it off".

?

cheers,

paulv
0
Reply Paul 4/22/2010 4:37:36 PM

On Apr 21, 4:17=A0pm, bala murugan <bala2...@gmail.com> wrote:
> Suppose i have the following,
>
> r =3D RANDOMU(SEED,1)
>
> and if the result is 0.423832.
>
> How do i round it off to 0.42 ??

When considering the all the answers given, it might be a good chance
to review the differences between ROUND, FLOOR, and CEIL.

try: print, round(r*100.)/100., floor(r*100.)/100., ceil(r*100.)/100.
0
Reply Bob 4/22/2010 4:59:33 PM

8 Replies
750 Views

(page loaded in 3.325 seconds)

Similiar Articles:













7/25/2012 12:45:08 PM


Reply: