1 2 Previous Next 18 Replies Latest reply: Jun 3, 2005 9:51 AM by 807597 RSS

    how to round a decimal number

    807597
      when the how can round a number like 12.43765765 to 12.43 or 12.4376,can I decide the decimal precision for it?
      and how to round it as a integer,make it 12.

      thanks
        • 1. Re: how to round a decimal number
          416044
          With the java.math.BigDecimal class:
              /**
               * Scale decimal number via the rounding mode BigDecimal.ROUND_HALF_UP.
               * 
               * @param value Decimal value.
               * @param scale New scale.
               * @return Scaled number.
               */
              static public double getScaled(double value, int scale) {
                  double result = value; //default: unscaled
          
                  //use BigDecimal String constructor as this is the only exact way for double values
                  result = new BigDecimal(""+value).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
                  
                  return result;
              }//getScaled()
          • 2. Re: how to round a decimal number
            416044
            To convert it to an integer, you can use

            Double.intValue()
            BigDecimal.intValue()

            or simply cast it to int: int n = (int) 12.43765765
            • 3. Re: how to round a decimal number
              807597
              Math.round()
              • 4. Re: how to round a decimal number
                JosAH
                Normally, rounding to a number of decimal places is only used for display
                purposes, so why not use a DecimalFormat class for these purposes?

                kind regards,

                Jos
                • 5. Re: how to round a decimal number
                  807597
                  Normally, rounding to a number of decimal places is
                  only used for display
                  purposes, so why not use a DecimalFormat class
                  for these purposes?

                  kind regards,

                  Jos
                  It requires less amount of typing to use Math.round() :P

                  Kind regards to Jos,
                  ***Annie***
                  • 6. Re: how to round a decimal number
                    JosAH
                    It requires less amount of typing to use Math.round() :P
                    Ok, you round a double number to, say seven decimal places then, using
                    Math.round; or better yet, round 0.1 to one decimal using Math.round ;-)
                    Kind regards to Jos,
                    ***Annie***
                    kind regards to you too,

                    Jos
                    • 7. Re: how to round a decimal number
                      807597
                      The question was on rounding to int. :P
                      Anyway, I don't mind rounding to decimals with that as well.

                      Kind regards to Grolsch,
                      ***Annie***
                      • 8. Re: how to round a decimal number
                        JosAH
                        The question was on rounding to int. :P
                        No it wasn't :-P look what the OP wrote:
                        when the how can round a number like 12.43765765 to 12.43 or 12.4376,can I
                        decide the decimal precision for it? and how to round it as a integer,make it 12.
                        Anyway, I don't mind rounding to decimals with that as well.

                        Kind regards to Grolsch,
                        ***Annie***
                        That would be a tat silly wouldn't it? Greeting a crate of Grolsch pffrrt: "Hello
                        Grolsch, how are you doing down there in that crate?" :-)

                        kind regards to your wallpaper ;-)

                        Jos
                        • 9. Re: how to round a decimal number
                          807597
                          The question was on rounding to int. :P
                          No it wasn't :-P look what the OP wrote:
                          Blame it on the Grolsch. :)
                          Kind regards to Grolsch,
                          ***Annie***
                          That would be a tat silly wouldn't it? Greeting a
                          crate of Grolsch pffrrt: "Hello
                          Grolsch, how are you doing down there in that crate?"
                          :-)

                          kind regards to your wallpaper ;-)

                          Jos
                          That was a transferred epithet Jos. Referring the keeper as the keep is not tat silly. :P

                          Anyway, my wallpaper has a picture of my dad. So, it wouldn't be silly.

                          Kind regards to Jos,
                          ***Annie***
                          • 10. Re: how to round a decimal number
                            807597
                            I tried this code but not every thing looks ok. No matter what you pick as your scale it will only return one decimal point. Anyway any idea !
                            • 11. Re: how to round a decimal number
                              807597
                              Post your code, using the code button!
                              • 12. Re: how to round a decimal number
                                807597
                                Math.round() only rounds to the nearest integer...what if you want to round to the nearest tenth...
                                Can you do that without making a new class, like with a simpler command?
                                • 13. Re: how to round a decimal number
                                  807597
                                  import java.text.DecimalFormat;
                                  
                                  class Rounding {
                                    public static void main(String[] args) {
                                      double number = 12.3456789;
                                      DecimalFormat df = new DecimalFormat("#.###");
                                      System.out.println(df.format(number));
                                    }
                                  }
                                  Outputs 12.346
                                  I think that is reasonable easy. Try changing the number of #'s after the decimal point and see what happens.
                                  • 14. Re: how to round a decimal number
                                    807597
                                    Thanks Flounder ,I had learned about "DecimalFormat" class
                                    1 2 Previous Next