0

I want to truncate the double.

e.g.

double d=3.123456789087654;

if I want to truncate It to the 10th digit after the decimal the result should be

result: 3.1234567890

I don't need round off value as a result

I tried my 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 get the appropriate result as I want.. can anybody please help me to clear my problem.

Does java supports such any function that supports truncation not round off??

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
kTiwari
  • 1,488
  • 1
  • 14
  • 21
  • Just out of curiosity, why would you want to lose precision? – Supericy Mar 04 '13 at 12:35
  • 1
    I'm on the side of this making little sense. Do you have a use case for rounding / truncating at a point other than when outputting the number? Also, while I have a hunch NPE is correct in that the "appropriate" result is a number that can't be represented exactly, can you say what's the actual problem with your code is? I.e. what's the wrong result for which input and what you've expected? – millimoose Mar 04 '13 at 12:48
  • @millimoose: my problem is simple..I want to turncate the double after a specific no after the decimal point. – kTiwari Mar 04 '13 at 13:13
  • @krishnaChandra No, I mean what are you ultimately trying to accomplish. *Why* are you trying to do this thing? Maybe there's a better way to achieve this. Keep in mind that there is no such thing as "digits after the decimal point" in a `double` - it's represented in memory as a sign+mantissa+exponent, the latter being numbers in base 2, not 10. The closest you can get without `BigDecimal`s is multiply by 10^digits, ceil/floor that, then divide again, and take whatever you get in the end. – millimoose Mar 04 '13 at 14:06
  • @krishnaChandra And even `BigDecimal`s with a limited scale aren't really a good answer, because *any and all* maths you're doing with those will skew the results in weird ways, and is probably a bad idea if you don't know what rounding error compounding is and how it works (I sure as hell don't remember) and why exactly you're deliberately introducing significant rounding errors into your calculations. So I'd say the answer to "I want to round numbers in the middle of a calculation, how do I do it?" is "No you don't, unless you can explain why." – millimoose Mar 04 '13 at 14:10

2 Answers2

4

In general, you can't, at least not by using double. The reason is that many numbers simply can't be represented exactly, even if they have a small number of decimal digits.

Take 0.1 as an example. The nearest double is 0.1000000000000000055511151... and no amount of truncation would give you exactly 0.1.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • yr point is correct ,,but as my problem goes..i want to truncate it upto a specified value.can it be done.. – kTiwari Mar 04 '13 at 13:00
0

You can use the DecimalFormat class.

double d = 3.123456789087654;

DecimalFormat newFormat = new DecimalFormat("#.##########");
double tenDecimal =  Double.valueOf(newFormat.format(d));

this will round the last digit.

I am agree with NPE. no amount of truncation would give you exactly 0.1.

only way is to convert in to string and after substring convert back to double.

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
  • @Hemant: u r correct ,as u hard coded the # signs in the DEciamlFormat method ,,in the second run if i would like to change the truncate point after decimal ,then this will not work ,,i will hv to change it explicitly – kTiwari Mar 04 '13 at 12:45