java - How to truncate the double value? -


this question has answer here:

i want truncate double.

e.g.

double d=3.123456789087654; 

if want truncate 10th digit after decimal result should be

result: 3.1234567890 

i don't need round off value result

i tried own function as

     static double truncatedouble(double number, int numdigits) {     double result = number;     string arg = "" + number;     int idx = arg.indexof('.');     if (idx!=-1) {         if (arg.length() > idx+numdigits) {             arg = arg.substring(0,idx+numdigits+1);             result  = double.parsedouble(arg);         }     }     return result ;  } 

but didn't appropriate result want.. can please me clear problem.

does java supports such function supports truncation not round off??

in general, can't, @ least not using double. reason many numbers can't represented exactly, if have small number of decimal digits.

take 0.1 example. nearest double 0.1000000000000000055511151... , no amount of truncation give 0.1.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -