Help Round decimal 2 palces
I have searched, but I cannot seem to find an answer to my question.
I am a student in into to programing and I am trying to figure out why my decimals will not round. I have tried everything I can find, but being new to java programing, I am sure I am making a syntatical error somewhere. My program works other than not displaying the decimal places the way I want, 200.00 not 200.0.
The following is my code, I have commented out each of the decimal formats I have tried.
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class CheckingAcct
{
//int decimalPlaces = 2;
// Truncates the big decimal value.
//bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
//String string = bd.toString();
//DecimalFormat df = new DecimalFormat ( "0.0#" );
//DecimalFormat twoDigits = new DecimalFormat ( "0.0#" );
//DecimalFormat decimalFormat = (DecimalFormat)numberFormat;
//decimalFormat.applyPattern("0.0#");
public static void main (String args[])
{
String strBalance,
strDeposit,
strChecks;
double balance = 200.00,
deposit,
lbcharge = 2.00,
nsfcharge = 25.00,
checks;
//Display Starting Balance
JOptionPane.showMessageDialog (null, "Your current balance is:$" + balance);
//calculate balance
strDeposit = JOptionPane.showInputDialog ("Please enter your deposit amount:$");
deposit = Double.parseDouble (strDeposit);
balance = deposit + balance;
JOptionPane.showMessageDialog (null, "Your current balance after deposit is:$" + balance);
strChecks = JOptionPane.showInputDialog ("Please enter the total amount of checks written:$");
checks = Double.parseDouble (strChecks);
balance = balance - checks;
//Low Balance
if (balance < 100.00)
{
JOptionPane.showMessageDialog (null, "You have fallen below the minium required balance and you have been charged:$" + lbcharge);
balance = balance - lbcharge;
}
else
{
lbcharge = 0.00;
}
//NSF Charge
if (balance <= 0.00)
{
JOptionPane.showMessageDialog (null, "You have a negative balance and you have been charged:$" + nsfcharge);
balance = (balance - nsfcharge);
JOptionPane.showMessageDialog (null, "Your current balance is:$" + balance);
}
else
JOptionPane.showMessageDialog (null, "Your balance is:$" + balance);
System.exit (0);
}
}
[2589 byte] By [
829a] at [2007-11-26 17:00:04]

Go back and read the API. You are on the right track with:DecimalFormat df = new DecimalFormat ( "0.0#" );But not quite there.
Thank you for the reply, could you possibly nudge me in the right direction, being new to this, I really do not wholely(sp?), understand what I am looking at? I have changed the format to:DecimalFormat df = new DecimalFormat ( "#.##" );
829a at 2007-7-8 23:27:50 >

> Thank you for the reply, could you possibly nudge me
> in the right direction, being new to this, I really
> do not wholely(sp?), understand what I am looking at?
> I have changed the format to:
>
> DecimalFormat df = new DecimalFormat ( "#.##" );
Are you reading the API?
I nudged you to the API where it is quite clear. Anymore help would be spoonfeeding, which really isn't helpful to either of us.
I tend to use super class NumberFormat, because I can never remember the jiggery-pokery of those patterns:
import java.text.*;
public class Example {
public static void main(String[] args) {
NumberFormat fmt = NumberFormat.getInstance();
fmt.setMinimumFractionDigits(2);
fmt.setMaximumFractionDigits(2);
System.out.println(fmt.format(200.0));
System.out.println(fmt.format(200.115));
System.out.println(fmt.format(200.125));
}
}
Just be aware that NumberFormat/DecimalFormat uses half-even rounding.
> > Thank you for the reply, could you possibly nudge
> me
> > in the right direction, being new to this, I
> really
> > do not wholely(sp?), understand what I am looking
> at?
> > I have changed the format to:
> >
> > DecimalFormat df = new DecimalFormat ( "#.##" );
>
> Are you reading the API?
>
> I nudged you to the API where it is quite clear.
> Anymore help would be spoonfeeding, which really
> isn't helpful to either of us.
What I was getting at is where can I find the api?
829a at 2007-7-8 23:27:50 >

http://java.sun.com/reference/api/
> I tend to use super class NumberFormat, because I can
> never remember the jiggery-pokery of those patterns:
> > import java.text.*;
>
> public class Example {
>public static void main(String[] args) {
>NumberFormat fmt = NumberFormat.getInstance();
>fmt.setMinimumFractionDigits(2);
>fmt.setMaximumFractionDigits(2);
>System.out.println(fmt.format(200.0));
>System.out.println(fmt.format(200.115));
>System.out.println(fmt.format(200.125));
> }
> }
>
> Just be aware that NumberFormat/DecimalFormat uses
> half-even rounding.
This confirms another thought I had, that I must format each number I want to use. Thank you.
829a at 2007-7-8 23:27:50 >

I Was not asking to be spoon feed zadok, I have had 3 classes and I am just learning.
DrLaszloJamf ,
I have read through this, http://java.sun.com/j2se/1.5.0/docs/api/java/text/DecimalFormat.html
As I am reading this, if I do not set the local it will apply the default, thus meaning I will not have to use the getInstance(loc); portion of the code?
829a at 2007-7-8 23:27:50 >

Yes, you can omit the locale.
DecimalFormat df = new DecimalFormat("0.00");will give you two decimal places
I used that before, but it is not giving me the decimals.
I am reading the api document, though I do not fully understand what I am reading. I see from DrLaszloJamf post, that I need to format each number, but I have not yet figured that out completely.
Message was edited by:
829
829a at 2007-7-8 23:27:50 >

It's not clear what you mean by "format each number"?Do you mean writingSystem.out.print(fmt.format(doubleValue));instead ofSystem.out.print(doubleValue);
>I used that before, but it is not giving me the decimals.
Are you sure? Try
package testing;
import java.text.DecimalFormat;
public class Test
{
public static void main(String[] args)
{
DecimalFormat df = new DecimalFormat("0.00");
double d = 567.1289756537;
System.out.println(df.format(d));
}
}
Jukka
> It's not clear what you mean by "format each
> number"?
> Do you mean writing
> > System.out.print(fmt.format(doubleValue));
>
> instead of
> > System.out.print(doubleValue);
>
I really not sure what I am getting at, other than each number I hardcode must have some type of print function. With only 3 classes under my belt, I may just be chewing off more than I can digest at this point. I am just trying to get ahead and I am sure we will go over this at some point this semester.
829a at 2007-7-8 23:27:50 >

I have spoken to someone about this and she said there is a different display option for the JOptionPane. The way I have this setup, it will display 200.00 in my capture output windown (using crimson,) but in my JOption.Pane window it will only show 200.00 as my starting balance.
829a at 2007-7-21 16:55:24 >

My updated code
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import java.text.NumberFormat;
public class CheckingAcct
{
//int decimalPlaces = 2;
// Truncates the big decimal value.
//bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
//String string = bd.toString();
/*DecimalFormat df = new DecimalFormat ( "0.00" );
double balance = 200.00;
System.out.println(df.format(balance));*/
//DecimalFormat twoDigits = new DecimalFormat ( "0.0#" );
//DecimalFormat decimalFormat = (DecimalFormat)numberFormat;
//decimalFormat.applyPattern("0.0#");
public static void main (String args[])
{
String strBalance,
strDeposit,
strChecks;
DecimalFormat df = new DecimalFormat ( "0.00" );
double balance = 200.00,
deposit,
lbcharge = 2.00,
nsfcharge = 25.00,
checks;
System.out.println(df.format(balance));
System.out.println(df.format(lbcharge));
System.out.println(df.format(nsfcharge));
/*double //balance = 200.00,
deposit,
lbcharge = 2.00,
nsfcharge = 25.00,
checks;
*/
//Display Starting Balance
JOptionPane.showMessageDialog (null, "Your current balance is:$" + balance);
//calculate balance
strDeposit = JOptionPane.showInputDialog ("Please enter your deposit amount:$");
deposit = Double.parseDouble (strDeposit);
balance = deposit + balance;
JOptionPane.showMessageDialog (null, "Your current balance after deposit is:$" + balance);
strChecks = JOptionPane.showInputDialog ("Please enter the total amount of checks written:$");
checks = Double.parseDouble (strChecks);
balance = balance - checks;
//Low Balance
if (balance < 100.00)
{
JOptionPane.showMessageDialog (null, "You have fallen below the minium required balance and you have been charged:$" + lbcharge);
balance = balance - lbcharge;
}
else
{
lbcharge = 0.00;
}
//NSF Charge
if (balance <= 0.00)
{
JOptionPane.showMessageDialog (null, "You have a negative balance and you have been charged:$" + nsfcharge);
balance = (balance - nsfcharge);
JOptionPane.showMessageDialog (null, "Your current balance is:$" + balance);
}
else
JOptionPane.showMessageDialog (null, "Your balance is:$" + balance);
System.exit (0);
}
}
829a at 2007-7-21 16:55:24 >

JOptionPane.showMessageDialog(null, "Your...is:$" + df.format(balance)); // <- format() returns a String reference
Thank you duckbill, that is all that was missing.Thank you all for getting me going in the right direction.
829a at 2007-7-21 16:55:24 >
