-3

input double number of 123456789 in test giving output of 1.23456789E8 using java.util.Scanner. I input this in the console to test the output it should be giving me the output of 123456789.0 . I am stumped I have looked and looked as to why this is happening and just cannot find a solution for it.

import java.util.Date;
import java.util.Scanner;



/**
 * Created by Jeremy Roth on 10/6/2016  PRG420 Java I.
 * What we need calculated:
 * salary (constant at $55,000.00 per year)
 * totalSales  (total sales for the year)
 * commissionPercentRate from totalSales (percentage of earnings from totalSales)
 * salary + yearlyCommission
 * output: yearlyCommission
 * output: salary + yearlyCommission = earningYearTotal (earnings for the year total)
 *
 * This is just a simple assignment for console output but my question/goal is what further examples or changes might
 * be necessary to convert this into GUI output later in the course?
 */

public class Main {

    private static final double salary = 55000.00;  //my attempt and making this immutable not sure if I'm right
    public static final double commissionPercentRate = 0.12; //my attempt and making this immutable not sure
    public double totalSales = 0; //console input by user
    public double yearlyCommission = 0;
    //private static DecimalFormat df2 = new DecimalFormat("###,###,###.##");


    // (2) The instance variables are visible for all methods, constructors and block in the class. Normally,
    // it is recommended to make these variables private (access level). However, visibility for
    // subclasses can be given for these variables with the use of access modifiers.



    public static void main(String[] args) {

        Date date = new Date();

        System.out.println("Welcome to the Sales Commission Calculator!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }

        System.out.println("Today is :" + date.toString());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            Thread.currentThread().interrupt();
        }

        System.out.println("Make sure you have your sales documentation ready!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        System.out.println("You will need your information handy to calculate your commissions!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the amount of your sales for the year: ");
        double number = scanner.nextDouble();
        System.out.println("The amount you entered in sales for the year is: $" + number);

        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        System.out.println("Your yearly salary is: $" + salary);



    }
}

CONSOLE OUTPUT:

Welcome to the Sales Commission Calculator!
Today is :Thu Oct 06 20:47:22 CDT 2016
Make sure you have your sales documentation ready!
You will need your information handy to calculate your commissions!
Enter the amount of your sales for the year: 
123456789
The amount you entered in sales for the year is: $1.23456789E8
Your yearly salary is: $55000.0

Process finished with exit code 0
Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
Javacodeman113
  • 407
  • 1
  • 4
  • 11

2 Answers2

0

While I would not recommend using the double primitive type to represent money (BigDecimal, int, or long would be more appropriate), you can use System.out.printf() to avoid seeing the exponential form:

System.out.printf("The amount you entered in sales for the year is: $%.2f", number);
Matthew Diana
  • 1,106
  • 7
  • 14
  • Thank you! My next step was to code a second version using the BigDecimal and you are very right indeed. It was a simpler mistake than I thought it was. – Javacodeman113 Oct 07 '16 at 02:19
0

1.23456789E8 is just scientific notation for 123456789

1.23456789E8 = 1.23456789 * 10^8 = 123456789

if you are saying you just want it to display as 123456789 in your output you can format it using printf instead of println:

System.out.printf("The amount you entered in sales for the year is: $%.0f\n", number);

credit to NPE for answering similar question on post:

How to print double value without scientific notation using Java?

Community
  • 1
  • 1
lllpratll
  • 368
  • 1
  • 3
  • 8