Results 1 to 7 of 7
  1. #1
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default 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. #4
    ahmedsameha1 is offline Member
    Join Date
    May 2014
    Posts
    32
    Rep Power
    0

    Default 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. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    18

    Default 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.

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

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default 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.

Similar Threads

  1. how can i format a double variable to currrency
    By mDennis10 in forum New To Java
    Replies: 4
    Last Post: 12-02-2011, 02:59 PM
  2. My double variable adds itself 0.032/33 and 0.064/65
    By Josep_16 in forum New To Java
    Replies: 3
    Last Post: 08-27-2011, 12:55 AM
  3. Double Variable Class
    By hypes057 in forum New To Java
    Replies: 9
    Last Post: 09-06-2009, 02:06 PM
  4. variable double problem
    By DarkoDrljaca in forum New To Java
    Replies: 5
    Last Post: 03-02-2009, 08:00 AM
  5. Replies: 3
    Last Post: 01-19-2008, 08:38 AM

Posting Permissions

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