# Thread: Truncate a double variable

1. Member
Join Date
May 2014
Posts
32
Rep Power
0

## Truncate a double variable

Hello all
Is there a method in Java API that truncate the float part of a double variable after a given number of digits?
Example: if the number of digits to be truncated is 2, so the method truncate (6.9872) to (6.98) ,
I tried to write a method that do that but it was very difficult.

2. ## Re: Truncate a double variable

Are you talking about the value of the variable: do these operations: multiply, cast and divide
or how the digits are shown as a String: use format() or printf()
Last edited by Norm; 01-13-2015 at 11:38 PM.

3. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Truncate a double variable

To obtain a string which is rounded to a given number of decimal places you can use the various printf() methods or the String method format().

Java Code:
```double easy = 3.14159;
System.out.printf("%f rounded is %.2f%n", easy, easy);```
See the Formatter documentation.

---

If it really is truncation you want, then have a read of this StackOverflow discussion which suggests

Java Code:
```DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.DOWN);
String truncated = df.format(6.9872);```
There are DecimalFormat methods to set the length of the float part if you don't always want 2. And negative numbers should be rounded down not up.

This also produces a String which is very probably what you need, and possibly what you want.

4. Member
Join Date
May 2014
Posts
32
Rep Power
0

## Re: Truncate a double variable

Thank you Norm and pbrockway2.
Sorry that my question was not clear.
I want a method that return double variable.
My try was the following code:
Java Code:
```        public double trucate( int t ) {
double s = uDouble % 1;
double b = (Math.pow(10, t) - 1);
double x = 0.0;
double l = s * Math.pow(10, t);
double n;
if (  Math.abs(l - b ) < 0.0001 || Math.abs(l - b ) < ( 0.0001 * 1/ Math.pow(10, t)) )
n = l;
else
n = (int) (l);

x = n / Math.pow(10, t);
double r = ( (int) (uDouble) ) + x;
return r;
}```
It is doing a good job, my question was about a method in Java API that do some thing like that.
Regards

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Truncate a double variable

So, did you read the StackOverflow discussion?

I ask because the code you posted truncates 9.62 to 2 decimal places as 9.61 (an example mentioned in the discussion). Norm's simpler version of the same thing has the same result. I won't enter into the controversy about whether this is wrong or merely rather surprising, but if you want the truncation to yield 9.62 then a robust method is given in that discussion.

 There is no method in the standard API to do this.
Last edited by pbrockway2; 01-14-2015 at 06:56 AM.

6. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
26

## Re: Truncate a double variable

The first question I would ask is "why".

If it's to display some result, then you need to be working with the format classes, or related string methods.
For anything else you might need to explain why you need this. For example if it's financial then either work with ints or BigDecimals.

7. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Re: Truncate a double variable

I agree with Tolls. And I really should have been clearer about that in post #3.

It's not that wanting a value formed from another one on a basis akin to everyday rounding or truncation is *never* right. But, on the basis of all the threads I have seen here, is not usually what is needed. A String (for display etc) is usually the correct type - ie the one that is most informative to a human reader - keeping the accurate double value preserved for later calculation.

On the one single occasion where I really wanted to use a rounded *value* I followed a procedure rather like that in the SO discussion, obtaining a String for display (which I also wanted) and then creating the value from that.

Whether you regard a truncation 9.62->9.61 as correct or not depends (like all correctness questions) on what you are trying to do. This is another reason to address Tolls' question.
Last edited by pbrockway2; 01-14-2015 at 08:17 PM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•