COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Rounding off

• Email
• 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

See related articles to this posting

```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

```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

```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

```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

```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

```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

```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

```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

8 Replies
1056 Views

Similar Articles

12/17/2013 10:31:23 PM
page loaded in 51769 ms. (0)

Similar Artilces:

Round and Round
I'm having a big problem trying to round some numbers with VB. If the value is .3456, I would like it to round to .3460. If the value is 1.382 I would like it to round to 1.380. Whatever the last digit is, it needs to end up as a zero (0) by either rounding up or down. The input can be a whole number or a fraction. I guess I'm not figuring the Round function out correctly. Help? Thanks. Me thinks I got it. :-0 "ProfitMax Trading Inc." <nospam@nospam.com> wrote in message news:jSzaf.7106\$CC.5354@dukeread09... > I'm having a big problem trying to rou...

Float.round
I am suggesting to change the Float.round function to use the well-known round-to-even method in case of a tie, i.e. x.5, where the distance to ceil and floor is equal. round currently uses a round-away-from-zero strategy. This strategy will introduce a bias when applied to many numbers, i.e. the average of the rounded numbers will be higher than that of original numbers. This effect does make a considerable difference, despite the singular nature of the x.5 values. Consider e.g. the computation of the median of an array with floats. Here is a quick hack ruby implementation which adds round ...

Hi There, I read out the content from a file like: foreach \$line (<\$handle>) { print \$line; sleep(1); } whixh works well so far. But what I would like is, if the loop gets to eof, it should start over on top again. How can i reset the reading pointer back to the beginning of the file? Thanks, -- roN On Mar 19, 11:43=A0am, cerr <ron.egg...@gmail.com> wrote: > Hi There, > > I read out the content from a file like: > foreach \$line (<\$handle>) { > =A0 =A0 =A0 =A0 =A0 print \$line; > =A0 =A0 =A0 =A0 =A0 sleep(1); > =A0 =A0 ...

Round
I have to round a vector X=[0.3 2.1 2.8 1] by another vector Y=[0.5 1 1.5 2 2.5] The result should be obviously z=[0.5 2 2.5 1] Which command I'd have to use? Thanks a lot YU=unique(Y); % not necessary if Y is strictly sorted Z = interp1(YU,YU,X,'nearest','extrap') % Bruno Fausto wrote: > I have to round a vector X=[0.3 2.1 2.8 1] by > another vector Y=[0.5 1 1.5 2 2.5] > > The result should be obviously z=[0.5 2 2.5 1] > > Which command I'd have to use? You'll have to write something to suit. -- ...

round
Hello I need w function to round a number to the decimal. For exapmle myfunction(100,2) results in 100.00 myfunction(10.4,3) results in 10.400 Tjanks Wojtek wrote: > I need w function to round a number to the decimal. > For exapmle > myfunction(100,2) results in 100.00 > myfunction(10.4,3) results in 10.400 http://groups.google.com/ and search for it. Wojtek wrote: > Hello > I need w function to round a number to the decimal. > For exapmle > myfunction(100,2) results in 100.00 > myfunction(10.4,3) results in 10.400 > > Tjanks > > If you want fo...

ROUND
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545 -".number_format(20.545, 2, ',', '.')."<br>"; echo "number_format 20.555 -".number_format(20.555, 2, ',', '.')."<br>"; PHP Version 4.3.0 / FreeBSD round 20.545 -20.55 round 20.555 -20.56 number_format 20.545 -20,55 number_format 20.555 -20,55 PHP Version 4.4.4 / CENTOS roun...

Rounding float in ruby via .round
round does not want arguments. Is there a reason why not? Ok, we can easily have workarounds by extending class Float. Or using another strategy. http://www.hans-eric.com/code-samples/ruby-floating-point-round-off/ But it made me actually wonder - is there a reason why .round does not want any arguments? My brain would easily tell me that I would expect 2.29801801942891420890142890124.round(3) to become 2.298 Is this not possible because it would become a different object? -- Posted via http://www.ruby-forum.com/. On 02.01.2010 02:43, Marc Heiler wrote: > .rou...

round cable vs non-round cable
performace wise, are round and not round cables equal on ultra 320 drives? ...

round-off
Hi guys, Is there any way to re-write or group the expression in the function below to make it less sensitive to round-off? Does it help any to break the large expression into smaller pieces? I suppose matlab already uses double precision. Is there any way to increase the precision beyond that? Best Regards, Peter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [res]=R_factor(n,m,rho) Rfac=0; for s=0:(n-abs(m))/2 Rfac=Rfac + (-1)^s *factorial(n-s)/(factorial(s)*factorial((n+abs(m))/2-s)*factorial((n-abs(m))/2-s))*rho^(n-2*s); end res=Rfac; %%%%%%%%%%%%%%%%%...

How ROUND?
Hi, i need to round: 3� decimal between 1 and 5 LOW 3� decimal between 6 and 9 UP Thanks Yachi (yachi@pointel.it) writes: > 3� decimal between 1 and 5 LOW > 3� decimal between 6 and 9 UP There is the round function: select (87.983, 2) However, the round() function will round 0.125 to 0.13, that is alwys up. This may or may not be a problem for you, dependning on the input source. If the input is decimal or money, they you need to write some ugly UDF to deal with the special case. But if the input is float, the issue is moot, because the likelihood that you will have a float valu...