JavaScript (Netbeans): Math & Decimal Format

Michael.Chute

Baseband Member
Messages
26
Location
United States
I need to allow the following programing steps to allow two decimal places not only at the end of every result but also allowed in the input portion up to two decimals. Example $1.99 + $1.99 = $3.98 as compared to using this and getting an error in return because I can't input decimals nor do I get a result of a decimal.
Code:
package storeapplication;

import java.util.Scanner;

public class StoreApplication {
    
    public static void main(String[] args) {
        
       System.out.printf( "%s\n%s\n%s\n\n",
          "----------Welcome to Michael's Natural Spring Water----------", "We buy natural spring water directly from Maine and stright \ninto the hands of the customer.", "PROMOTION: 10% off your purchase of more than 100 pounds of \nMichael's Natural Spring Water");

       Scanner input = new Scanner( System.in );
       
       int PriceBeforeTaxes;
       int QuantityOfItem;
       int ItemWeight;
       int PricePerPound;
       int TotalPriceWithTaxesFraud;
       int TaxRate;
       int TotalPriceWithTaxesActual;
       
       System.out.printf("Quantity of item?                                                                                X  ");
       QuantityOfItem = input.nextInt();
       
       System.out.printf("Weight of item in pounds?                                                                      LBS  ");
       ItemWeight = input.nextInt();
       
       System.out.printf("Price per pound of item?                                                                     $/LBS  ");
       PricePerPound = input.nextInt();
       
       if( ItemWeight <= 100){
           PriceBeforeTaxes = QuantityOfItem*ItemWeight*PricePerPound;
           System.out.printf("Your total before taxes comes out to...                                                          = $%d.00\n\n", PriceBeforeTaxes);
           
           System.out.printf("What is your local tax rate?                                                                       %%");
           TaxRate = input.nextInt();
           TotalPriceWithTaxesFraud = PriceBeforeTaxes*(1+TaxRate);
           TotalPriceWithTaxesActual = ((TotalPriceWithTaxesFraud-1)/100)+PriceBeforeTaxes;
           System.out.printf("\nYout total with taxes comes out to...                                                  SUB TOTAL = $%d\n\n", TotalPriceWithTaxesActual);}
       
       if( ItemWeight >= 101){
           PriceBeforeTaxes = (QuantityOfItem*ItemWeight*PricePerPound)-((QuantityOfItem*ItemWeight*PricePerPound)/10);
           System.out.printf("Your total with a 10%% discount before taxes comes out to...                                      = $%d.00\n\n", PriceBeforeTaxes);
                         
           System.out.printf("What is your local tax rate?                                                                       %%");
           TaxRate = input.nextInt();
           TotalPriceWithTaxesFraud = PriceBeforeTaxes*(1+TaxRate);
           TotalPriceWithTaxesActual = (((TotalPriceWithTaxesFraud-1)/100)-((TotalPriceWithTaxesFraud-1)/100)/10)+PriceBeforeTaxes;
           System.out.printf("\nYout total with a 10%% discount taxes comes out to with a 10%% discount...               SUB TOTAL = $%d\n\n", TotalPriceWithTaxesActual);}              
    }
}

Note: The following line are supposed to be used according to the teacher but since I couldn't figure out how to get it to give me the outcome I needed I coded with it and worked around it by reconfiguring the mathematics and redoing the problem in a following problem. So please disregard.
TotalPriceWithTaxesFraud = PriceBeforeTaxes*(1+TaxRate);
 
That's Java, not JavaScript (two different languages).

Instead of using Int, why not use Double for all of your prices and tax rates?

Integer will cut off your decimal points. Double is generally used for general decimal usage; floats are used for precision decimal calculations.
 
That's Java, not JavaScript (two different languages).

Instead of using Int, why not use Double for all of your prices and tax rates?

Integer will cut off your decimal points. Double is generally used for general decimal usage; floats are used for precision decimal calculations.

I heard about DOUBLE and FLOAT but I wasn't sure how to use them, do I just change all the INTs to DOUBLE or FLOAT or is there more to it? Should I use FLOAT since I want it to calculate the most accurate cost?
 
Float should only be necessary with high-precision decimals (i.e. scientific calculations).

Doubles are fine for general purpose decimals (i.e. prices, etc.). So for your case, a double is fine.

You don't need to change everything to a double. Everything that is a whole number (i.e. quantities) can stay an integer.

Sorry about that I assumed they were the same thing.

Javascript is a web-language, used on webpages ;). Java is an application language.
 
Float should only be necessary with high-precision decimals (i.e. scientific calculations).

Doubles are fine for general purpose decimals (i.e. prices, etc.). So for your case, a double is fine.

You don't need to change everything to a double. Everything that is a whole number (i.e. quantities) can stay an integer.



Javascript is a web-language, used on webpages ;). Java is an application language.

Will try and get back to show my final result.
 
When I replace int with double on the following;
double PriceBeforeTaxes;
int QuantityOfItem;
int ItemWeight;
double PricePerPound;
int TotalPriceWithTaxesFraud;
int TaxRate;
double TotalPriceWithTaxesActual;
I receive an "error possible loss of precision", "required: int", "found: double".
 
You're probably assigning one of the double variables to an integer variable.

You should also change your TotalpriceWithTaxesFraud and TaxRate into doubles as well.

Does it tell you the line number that error is occurring on?
 
When you write the equations add .00 to the numbers that are constant (i.e.((TotalPriceWithTaxesFraud-1)/100)+PriceBeforeTaxes) change the 1 to 1.00. It may seem weird but it can cause your values to be changed to integers since you are dividing by and integer. I am not sure if this is true in java it has been a while since I wrote a java app, but other languages will lose precision if you divide or multiply by a value that is not a double or float.
 
Last edited:
Back
Top Bottom