Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding a simple program  RSS feed

 
Akshit dhar
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get value of double data type to be stored in an integer without loosing the precision.

double d=5.874;

I want the same value in integer variable ( say i=5874).
Is it possible? If yes, then How?

Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't, in general.

A double contains 64 bits. An int contains 32 bits.
 
Akshit dhar
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any data type we can convert double data type to, in order to get the solution?
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly is the reason you want to do this? Can you describe the use case?
 
Akshit dhar
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this value d=5.738
I just want the decimal to be removed.
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but you didn't explain why.
 
Akshit dhar
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a program and it's giving output in decimals which i don't want. I want integer value of the result.
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you just want to truncate the decimal part? In the beginning, you said you didn't want to lose precision.

What does the value represent? Why are the decimals unwanted? We can help you a lot better if you help us understand the purpose of your application.
 
anotida Maditsha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write a program that iterate though the double value and picking integer numbers into string which then converts the string to integer.
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anotida, what would the int value of Double.MAX_VALUE be?

Any solution proposed at this point may be inappropriate if we don't know what the purpose of the application is.
 
anotida Maditsha
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Huls thanks hadn't consider that. Akshit dhar can you give a hint of the program.
 
Salil Wadnerkar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read up on BigDecimal (and also read up on why Double can represent much larger values than Integer and Long)
 
Akshit dhar
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the whole program :
//Input is 0 1 10
//Output is 8.426661309628124E22

Here's the thing, instead of that value 8.426661309628124E22, i want 8426661309628124 , Also i don't want Exponential (E22). Just the integer value.


 
Salil Wadnerkar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the binary representation of a double value using: https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#doubleToLongBits(double)
IEEE 754 floating-point "double format" details are here:
https://docs.oracle.com/cd/E19957-01/806-3568/ncg_math.html
So, you can extract just the value of the fraction and process it.
 
Stephan van Hulst
Saloon Keeper
Posts: 7805
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't seem like you're actually performing any operations for which you require a fractional part. Modify your application to use BigInteger and you're done.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akshit dhar wrote:Here's the thing, instead of that value 8.426661309628124E22, i want 8426661309628124 , Also i don't want Exponential (E22). Just the integer value.

Again: Why?

It can be done, but it strikes me as a wholly useless exercise, since it removes a major piece of information about magnitude.

Unless, of course, you've been told to do it (in which case it might be worth asking the same question of the person who told you to).

However, if you absolutely feel you must do it, I'm with Salil - read up on BigDecimal, and specifically, its unscaledValue() and stripTrailingZeros() methods.

And even then, it may not be as straightforward as you think...

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!