• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Working with Decimal values

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am working on a program that reads in decimal values.
I have the input set as Double.
for example : 23.0, 23.0, 24.0, 24.0, 25.0, 25.0, 25.234, 25.234, 25.342, 25.342, 26.0

I am trying to perform different functions depending on the value of the data.
For example if 23.0 I want to do function 1.
for value 25.234 I want to do function 2.

What is the best way to differentiate the whole number value from the value with numbers to the right of the decimal.

Thanks
Rudy
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because of the imprecision always associated with floating point arithmetic, there is probably not a "best" way, but have you tried
. . . if (number % 1 == 0.0) . . .
?
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also want to look at BigDecimal and use the scale method to check how many numbers are to the right of the decimal point.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic