Velocity Reviews > Java > maths

# maths

focode
Guest
Posts: n/a

 08-29-2008
when i divide 5/2 in java i get 2.0 how can i get the exact value
2.5 , actually i want to generate random number from 0 to 1 like .
00987 , .09654 ........ pls any one reply me soon

kapkan
Guest
Posts: n/a

 08-29-2008
focode пишет:
> when i divide 5/2 in java i get 2.0 how can i get the exact value
> 2.5 , actually i want to generate random number from 0 to 1 like .
> 00987 , .09654 ........ pls any one reply me soon

int five = 5;
int two = 2;
double result = five/twoint two = 2; // result = 2

double result = (double)five/two // result = 2.5

lscharen@d.umn.edu
Guest
Posts: n/a

 08-29-2008
On Aug 29, 12:47*am, focode <(E-Mail Removed)> wrote:
> 2.5 , actually i want to generate random number from 0 to 1 like .
> 00987 , .09654 ........ *pls any one reply me soon

import java.util.Random;

Random rng = new Random();
double randomNumber = rng.nextDouble();

-Lucas

kapkan
Guest
Posts: n/a

 08-29-2008
focode пишет:
> when i divide 5/2 in java i get 2.0 how can i get the exact value
> 2.5 , actually i want to generate random number from 0 to 1 like .
> 00987 , .09654 ........ pls any one reply me soon

int five = 5;
int two = 2;
double result = five/two // result = 2

double result = (double)five/two // result = 2.5

kapkan
Guest
Posts: n/a

 08-29-2008
focode пишет:
> when i divide 5/2 in java i get 2.0 how can i get the exact value
> 2.5

int five = 5;
int two = 2;
double result = five/two // result = 2

double result = (double)five/two // result = 2.5

Roedy Green
Guest
Posts: n/a

 08-29-2008
On Thu, 28 Aug 2008 22:47:09 -0700 (PDT), focode
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>when i divide 5/2 in java i get 2.0 how can i get the exact value
>2.5 , actually i want to generate random number from 0 to 1 like .
>00987 , .09654 ........ pls any one reply me soon

see http://mindprod.com/jgloss/floatingpoint.html
--

The Java Glossary
http://mindprod.com

Roedy Green
Guest
Posts: n/a

 08-31-2008

>>when i divide 5/2 in java i get 2.0 how can i get the exact value
>>2.5 , actually i want to generate random number from 0 to 1 like .
>>00987 , .09654 ........ pls any one reply me soon

also see http://mindprod.com/jgloss/pseudorandom.html
--

The Java Glossary
http://mindprod.com