• Post Reply Bookmark Topic Watch Topic
  • New Topic

[ . . . and thanks]how to make from double "235.97999999993036" to double "235.97"  RSS feed

 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can somebody help me in the subject ?
Thanks in advance.
 
Vishal Lad
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried parsing it to float?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


There is no such math function which can solve this problem.
Float conversion does what I need, I havent tried yet.

 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Lad wrote:Have you tried parsing it to float?

Thanks Im going to try it.
Conversion with setting of precision would be better, but thanks really.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Don't you have it the wrong way around? num / 100.0 is 2.3597999999993036, so flooring that and then multiplying gives you 200.0. Math.floor(num * 100.0) / 100.0 first floors 23597.999999993036 to 23597, then divides by 100.0 to give you 235.97.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


I have noticed as well .
 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips guys!
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we do not want mathematically rounding-up (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?
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In theory, but remember that doubles don't have infinite precision, so not all values can be represented exactly.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be some useful Math functions to do this, I haven't looked, but

also does the job nicely.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's the more common way to do this, outside Java at least. I went with the Math functions in order to make it easier to switch from floor() to round(), if that was desired. Of course proper rounding can also be achieved by adding .5, but the method calls are probably more readable.
 
Csaba Szegedi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinoth Kumar Kannan wrote:If we do not want mathematically rounding-up (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?


Because the solution you mentioned seems to be ineffective in large number operations.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

also does the job nicely.
Does it still work nicely when you put my line before it?
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought of that after posting... try long instead of int...
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... then realise that if your number's even bigger this won't work either! Best stick with Math.floor
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: . . .. . .
. . . and now what do you get when you put those two lines together ? Even with the correction noticed later.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote:... then realise that if your number's even bigger this won't work either! Best stick with Math.floor
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.

There is a way to do it in Java™ which will even work for 19-digit numbers and negative numbers. For which I shall take the "SOLVED" off the thread title
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Mike Simmons wrote: . . .. . .
. . . and now what do you get when you put those two lines together ?

To avoid further confusion, I have inserted the correction.

Well, the original poster would need to specify what's the desired behavior for negative numbers. I don't really care, as anything other than proper rounding seems fundamentally wrong to me. It's easy to insert an if (num < 0) if that's actually an issue.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is a way to do it in Java™ which will even work for 19-digit numbers and negative numbers. For which I shall take the "SOLVED" off the thread title

These trivial variations are getting boring.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Luigi Plinge wrote:... then realise that if your number's even bigger this won't work either! Best stick with Math.floor
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.

But given what you say, you'd already have lost that precision as soon as you assigned that number to a double. The issue is more that for very large numbers, they simply don't fit in a long, and the double you get back can only be as big as a long, which might be incorrect by orders of magnitiude.

There is a way to do it in Java™ which will even work for 19-digit numbers and negative numbers. For which I shall take the "SOLVED" off the thread title

I guess you're thinking of something better than
?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote: . . . I guess you're thinking of something better than . . .?
Yes. And it will work for 19 digits. Or 91 digits. That latter bit should give you a hint.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the Math.floor function works for arbitrarily large doubles, but it's pointless since you don't have decimals when the doubles become very large, due to precision.

If you're using 91 digit numbers (which is not what the OP wants) you'd use BigDecimal.

I tried


but this comes back with the result
999999777777777727950645471104033750875663508894121984.00
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dumb, I shouldn't have assigned it to a double first... what you'd want is:


Which gives 999999777777777777777888888888888888888888888888888888.13
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change RoundingMode.UP to RoundingMode.DOWN in OP's case, obv.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RoundingMode.DOWN, surely?

But that is what I was thinking of
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!