5 Replies Latest reply: Feb 27, 2009 8:17 AM by 807588 RSS

    How to print numeric string as decimal

    712940
      Hi All,

      I am trying to print a numeric interger which included 2 decimal place without much success so far. Below is the command that I using:
      System.out.printf("$%5.2f\n" ,Float.parseFloat(12345));
          System.out.println("$" + decFor.format(Double.parseDouble(12345));
      But both statements produces 12345.00. However, I would like it to print $123.45 instead. ie use the last 2 digits as decimal.

      Any assistance would be much appreciated.

      Many thanks,

      Jack
        • 1. Re: How to print numeric string as decimal
          807588
          long foo = 9223372036854775807L;
          System.out.printf("$%d.%02d%n", foo / 100, foo % 100);
          System.out.printf("$%5.2f\n" ,foo / 100.);
          Edited by: pbrockway2 on Feb 26, 2009 8:15 PM

          A couple of other things - since I've just noticed that D$10.00 are at stake!

          When you use %f with printf() the "f" stands for "floating point" - it does not mean that you need to provide a Float argument. A double (or float) argument is fine. In fact an int argument is quite all right. It's the format which is floating point.

          %n is like \n but it provides the right sort of newline for the OS that it's run on.

          I posted a solution with / and % because you already start with an integer value. In other words, there is no question of "precision" so the neatest solution (it seems to me) would be to avoid things like %.2f.

          Edited by: pbrockway2 again, to remove a mind-typo. (hence the forward reference)
          • 2. Re: How to print numeric string as decimal
            807588
            Did you want to achieve this?
            // dividing by 100. (point is to convert to float)
            System.out.printf("$%5.2f\n" ,12345 / 100.);
            • 3. Re: How to print numeric string as decimal
              JoachimSauer
              sztyopek wrote:
              // dividing by 100. (point is to convert to float)
              I'd suggest using 100f or 100d instead. That makes it more obvious that you want a floating point number here.
              • 4. Re: How to print numeric string as decimal
                712940
                Hi All,

                You are all correct and I used the following solution which worked beautifully:
                int cash = 12345;
                System.out.printf("$%5.2f\n" ,cash / 100f.) which gives $123.45.
                This is exactly what I was looking for.

                Thank you everyone for your valuable analysis and advice,

                Jack
                • 5. Re: How to print numeric string as decimal
                  807588
                  If you're looking to format currency, you should probably simply use:
                  int cash = 12345;
                  NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
                  System.out.println(currencyFormat.format(cash / 100.0));
                  ~