My programs can be found at www.mycoding.net
Campbell Ritchie wrote:"Parsing" to a float won't work; that would give 235.98. Did you mean casting?
By the way, your double isn't "235.97999999993036". It's 235.97999999993036. No "". Look at the methods of the java.lang.Math class. I can think of a possible candidate there.
Vishal Lad wrote:Have you tried parsing it to float?
Mike Simmons wrote:Hmmm, it's just simple math, no?
Personally I think Math.round() would almost certainly make more sense than Math.floor(), but that doesn't match the result you asked for.
SCJP 1.4  SCJP 6  SCWCD 5  OCEEJBD 6  OCEJPAD 6
How To Ask Questions How To Answer Questions
Mike Simmons wrote:Yeah, I flipped the * and /. I, um, did that as an exercise for the readers, to keep them on their toes. Yeah, that's it.
Vinoth Kumar Kannan wrote:If we do not want mathematically roundingup (235.97999 to become 235.98), then why not convert it to a string, substring it & parse it back to a double. We would land up with 235.97, wouldn't we?
Does it still work nicely when you put my line before it?
also does the job nicely.
. . . and now what do you get when you put those two lines together ? Even with the correction noticed later.Mike Simmons wrote: . . .. . .
A long holds up to 19 digits and a double is precise to a maximum of about 15.7 digits. So when you are trying to cast 123456789012345678.9 to a long, the last 678.9 will vanish into imprecision.Luigi Plinge wrote:... then realise that if your number's even bigger this won't work either! Best stick with Math.floor
Campbell Ritchie wrote:
. . . and now what do you get when you put those two lines together ?Mike Simmons wrote: . . .. . .
Campbell Ritchie wrote:There is a way to do it in Java™ which will even work for 19digit numbers and negative numbers. For which I shall take the "SOLVED" off the thread title
Campbell Ritchie wrote:
A long holds up to 19 digits and a double is precise to a maximum of about 15.7 digits. So when you are trying to cast 123456789012345678.9 to a long, the last 678.9 will vanish into imprecision.Luigi Plinge wrote:... then realise that if your number's even bigger this won't work either! Best stick with Math.floor
There is a way to do it in Java™ which will even work for 19digit numbers and negative numbers. For which I shall take the "SOLVED" off the thread title
Yes. And it will work for 19 digits. Or 91 digits. That latter bit should give you a hint.Luigi Plinge wrote: . . . I guess you're thinking of something better than . . .?
Today you are you, that is turer than true. There is no one alive who is youer than you!  Seuss. Tiny ad:
The trailboss has a kickstarter
https://coderanch.com/t/754577/GardenMasterkickstarter
