Javacodeman113 Javacodeman113 - 2 months ago 14
Java Question

input double number of 123456789 in test giving output of 1.23456789E8 using java.util.Scanner

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

Answer

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);