Andrew King
Andrew King

Reputation: 15

Formatting of double to not include decimal places

The code below is what I have. I am a fairly new programmer going through my first Java class so bear with me.

import salespersonannualcomp.SalespersonCompensationAnnualCalculator;
import java.util.Scanner;

public class SalespersonAnnualSalesInput
{
    public static void main( String[] args)
    {
        Scanner input = new Scanner( System.in );

        //Instantiates a new instance of SalespersonCompensationAnnualCalculator
        SalespersonCompensationAnnualCalculator myAnnualSales = new SalespersonCompensationAnnualCalculator();

        //Prompt for and input total annual sales
        System.out.println( "Please enter the total annual sales:" );

        String yearlySalesString = input.nextLine();
        //Declares yearlySalesInt as the variable that will store the results of Integer.parseInt
        int yearlySalesInt = Integer.parseInt(yearlySalesString);

        //Declares calcResults as the variable that will store the value created by the calcAnnualCompensation method
        double calcResults = myAnnualSales.calcAnnualCompensation(yearlySalesInt);

        //Displays the result of the calculations done for determining total annual compensation
        System.out.println(" Total Annual compensation is $"+ calcResults);
        System.out.println();
        System.out.println(" Total Potential Annual Compensation Chart");

          for(double potentialAnnualSales = yearlySalesInt;potentialAnnualSales<=(1.5*yearlySalesInt);potentialAnnualSales=potentialAnnualSales+5000)
          {
              double calcAnnualCompensation=myAnnualSales.calcAnnualCompensation(potentialAnnualSales);

              System.out.printf( "%f %f%n ",potentialAnnualSales,calcAnnualCompensation);

          }
    } 
}

The resulting output looks like this.

Total Annual compensation is $60400.0

Total Potential Annual Compensation Chart
160000.000000 60400.000000
 165000.000000 60725.000000
 170000.000000 61050.000000
 175000.000000 61375.000000
 180000.000000 61700.000000
 185000.000000 62025.000000
 190000.000000 62350.000000
 195000.000000 62675.000000
 200000.000000 63000.000000
 205000.000000 63325.000000
 210000.000000 63650.000000
 215000.000000 63975.000000
 220000.000000 64300.000000
 225000.000000 64625.000000
 230000.000000 64950.000000
 235000.000000 65275.000000
 240000.000000 65600.000000

I would like for it to not display the decimal places, and for all the lines to align correctly. I have the output correct but I'm struggling with the formatting.

Upvotes: 0

Views: 67

Answers (4)

5gon12eder
5gon12eder

Reputation: 25459

You can tell printf how many decimal places to print as well as how wide to make each field. The syntax is like so: %<WIDTH>.<PRECISION>f where <WIDTH> and <PRECISION> are to be replaced by non-negative integers. Both numbers are optional and you can leave them out if you want the defaults.

Here I am printing some random numbers with column width of 10 characters and a precision of 2 decimal places.

public class Test {
    public static void main(String[] args) {
        for (int i = 0; i < 6; ++i) {
            System.out.printf("%10.2f%10.2f%10.2f%10.2f%n",
                              100000.0 * Math.random() * Math.random(),
                              100000.0 * Math.random() * Math.random(),
                              100000.0 * Math.random() * Math.random(),
                              100000.0 * Math.random() * Math.random());

        }
    }
}

Possible Output:

  72411.07  11074.66   4722.24  74523.64
    264.89  54015.77  53969.66  61229.94
   5386.74   7939.65  47678.67  24953.68
   4985.14  17769.77  17345.57  38392.68
   4841.93   4103.14   3581.99  74036.73
  52477.30   1846.34  35547.62  10065.36

If I change the format specifiers from %10.2f to %10.0f it will only print the integral part and I might get the following output instead:

     42116     26756      3293      7957
      1693     23516     83116     39032
      3981     40417     53635     19735
     53504     77468     12341     16178
      4424     81325     79304      5460
     23825      6004     16507     37537

For more advanced features of printf, see the documentation of java.util.Formatter.

Upvotes: 0

Todd
Todd

Reputation: 31720

You are close...

If you want no places after the decimal, and to line it up:

System.out.printf( "%10.0f %10.0f%n ",potentialAnnualSales,calcAnnualCompensation);

If you want two places after the decimal:

System.out.printf( "%10.2f %10.2f%n ",potentialAnnualSales,calcAnnualCompensation);

Essentially, 10.2f means 10 spaces to the left of the decimal (using spaces for padding) and 2 after. Similarly, 6.0f would mean 6 spaces to the left of the decimal and none after. You'll have to play with the number of spaces for your specific use case.

Upvotes: 1

Jeremy Farrell
Jeremy Farrell

Reputation: 1491

In general using double's for monetary calculations is a bad practice. Instead use BigDecimal. To get rid of the trailing decimals call setScale(0); on your instance. For better formatting use NumberFormat.

Upvotes: 1

Bohemian
Bohemian

Reputation: 425428

Use either:

new DecimalFormat("#").format(d)

Or:

(int)Math.round(d)

Upvotes: 1

Related Questions