New to Java Need 2 decimal places, please :). Here is my code
Code Java:
import java.io.*;
public class Balance
{
public static void main(String[] args) throws IOException
{
BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
// declare and construct variables
String sBeginningBalance, sTotalDeposits, sTotalChecks, sTotalFees;
float fBeginningBalance, fTotalDeposits, fTotalChecks, fTotalFees;
float fEndingBalance = 0;
//prompts and gets input
System.out.println("BALANCING YOUR CHECKBOOK");
System.out.print("Enter the balance of your last statement? $");
sBeginningBalance = dataIn.readLine();
fBeginningBalance = Float.parseFloat(sBeginningBalance);
System.out.print("What is the total amount of all deposits? $");
sTotalDeposits = dataIn.readLine();
fTotalDeposits = Float.parseFloat(sTotalDeposits);
System.out.print("What is the total amount of all checks? $");
sTotalChecks = dataIn.readLine();
fTotalChecks = Float.parseFloat(sTotalChecks);
System.out.print("What is the amount of all transaction fees? $");
sTotalFees = dataIn.readLine();
fTotalFees = Float.parseFloat(sTotalFees);
//Data conversion
double dBeginningBalance = fBeginningBalance;
double dTotalDeposits = fTotalDeposits;
double dTotalChecks = fTotalChecks;
double dTotalFees = fTotalFees;
fEndingBalance = (fBeginningBalance + fTotalDeposits - fTotalChecks - fTotalFees);
System.out.print("Your new balance is: " + fEndingBalance + ".");
}//end main
}//end class
Re: New to Java Need 2 decimal places, please :). Here is my code
Code Java:
//import the DecimalFormat class..
import java.text.DecimalFormat;
Add this piece of code before printing out the result :
Code Java:
DecimalFormat decimalFormat = new DecimalFormat("#.##");
//change the print statement to the below line..
System.out.print("Your new balance is: " + Double.valueOf(decimalFormat.format(fEndingBalance)) + ".");
P.S: Use code tags next time you post. Also, post under right section.
Re: New to Java Need 2 decimal places, please :). Here is my code
Welcome to the forums Charyl.
In future, please give us as much information as possible about your issue. Posting code without any background information means you limit the replies you will receive.