I have a Double object that loses the exact value when converted to a long value.
Double
long
Double d = 1.14*100 System.out.println(d.longValue());
The above statement will print: 113 .
113
I want to print 114 .
114
thanks
If you need the exact value of 114 that you used to use Math.round :
Math.round
double d = 1.14*100; System.out.println(Math.round(d));
If you are looking for a representation of an integer / long value with the value you expect, you can use this:
Math.round(1.14 * 100)
Firstly, a double not exact. Then, when double / float pressed on a long / int , the decimal part is discarded (not rounded).
double
float
int
To get the closest value, you need to round it:
System.out.println(Math.round(d));
try it
Long.parseLong( String.format( "%.0f",doublevalue ) ) ;
Source: https://habr.com/ru/post/957705/More articles:log file is empty when using log4j2 - javaHow to encrypt user.settings parameters - c #log4j2: location to set the Log4jContextSelector system property for asynchronous logging - javafor debugging a loop in java - value overflow - javaWhy is size always = 4096 in a Linux character character read call? - cIdentify a specific charger input - androidGradle: IntelliJ as an βexportβ dependency? - android-studioCross-origin error getImageData - javascriptAzure website remote debugging using git not working - azureBreeze compatible GUI JavaScript - angularjsAll Articles