Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Can somebody help me in the subject ?
Thanks in advance.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried parsing it to float?
 
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4286
58
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tips guys!
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 14327
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In theory, but remember that doubles don't have infinite precision, so not all values can be represented exactly.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought of that after posting... try long instead of int...
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4286
58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change RoundingMode.UP to RoundingMode.DOWN in OP's case, obv.
 
Campbell Ritchie
Marshal
Posts: 76492
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RoundingMode.DOWN, surely?

But that is what I was thinking of
 
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/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic