Re: Rounding off [message #70590] |
Thu, 22 April 2010 09:59 |
Bob[3]
Messages: 60 Registered: December 2006
|
Member |
|
|
On Apr 21, 4:17 pm, bala murugan <bala2...@gmail.com> 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 ??
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.
|
|
|
Re: Rounding off [message #70591 is a reply to message #70590] |
Thu, 22 April 2010 09:37  |
Paul Van Delst[1]
Messages: 1157 Registered: April 2002
|
Senior Member |
|
|
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
|
|
|
Re: Rounding off [message #70607 is a reply to message #70591] |
Wed, 21 April 2010 14:52  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
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.")
|
|
|
Re: Rounding off [message #70608 is a reply to message #70607] |
Wed, 21 April 2010 14:48  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
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.")
|
|
|
Re: Rounding off [message #70609 is a reply to message #70608] |
Wed, 21 April 2010 14:39  |
Carsten Lechte
Messages: 124 Registered: August 2006
|
Senior Member |
|
|
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
|
|
|
Re: Rounding off [message #70610 is a reply to message #70609] |
Wed, 21 April 2010 14:20  |
cgguido
Messages: 195 Registered: August 2005
|
Senior Member |
|
|
On Apr 21, 3:50 pm, David Fanning <n...@dfanning.com> wrote:
> 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.")
perhaps this will do it?
print, round(f*100.)/100.
|
|
|
Re: Rounding off [message #70611 is a reply to message #70610] |
Wed, 21 April 2010 13:50  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
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.")
|
|
|
Re: Rounding off [message #70612 is a reply to message #70611] |
Wed, 21 April 2010 13:49  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
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.")
|
|
|