1

For a project I have a program that is like a bank. The "bank" reads in a file with accounts in it that look like this:
Miller
William
00001
891692 06 <----string that needs to be converted to double
The last string has to be converted to a double so that the program can perform calculations on it like addition and subtraction, etc.
And also I have to print it out in monetary format, i.e. $891692.06

I have this so far:

 public class Account {
      private String firstName, lastName;
      private int accountID;
      private String currentBalance; 

      private static int maxAccountID;

      public Account(String fN, String lN, int acct, String bal) {
         firstName = fN; lastName = lN;
         accountID = acct;
         currentBalance = bal;
         if(accountID > Account.maxAccountID)
            Account.maxAccountID = accountID;
      }     

  public double getBalance(){
        String [] s = currentBalance.split(" ");
        String balStr = "$"+s[0]+"."+s[1];
            double currentBalance = Double.parseDouble(balStr);
         return currentBalance;
      }
}

Thanks in advance!

1
  • 3
    What exactly is your question? In your code already you convert a String to a double with Double.parseDouble(balStr) Commented Oct 31, 2012 at 19:50

4 Answers 4

14

If your string representing double uses space ' ' instead of a decimal point, you can fix it by replacing the space with a dot, like this:

String currentBalanceStr = "891692 06";
double currentBalanceDbl = Double.parseDouble(currentBalanceStr.replaceAll(" ","."));

Link to ideone.

Sign up to request clarification or add additional context in comments.

8 Comments

With this it's returning the amounts as '0.0'
@Gcap That is strange. Did you follow the link to see if your code does the same thing as mine?
Yes it looked at that.. the thing is that I have a string being inputted through currentBalanceStr from another class
@Gcap You should print currentBalanceStr to see its value before calling parseDouble then.
Nvmd its working lol but when the String is say "891692 60" it cuts off the 0 at the end, do you know of any way I can keep it?
|
4

Using Double for balance calculation is not a good idea, it makes more sense to use the BigDecimal because it is more precise.

You can also add the currency using the class bellow:

Locale enUSLocale =
    new Locale.Builder().setLanguage("en").setRegion("US").build();

Currency currencyInstance = Currency.getInstance(enUSLocale);

System.out.println(
    "Symbol for US Dollar, en-US locale: " +
    currencyInstance.getSymbol(enUSLocale));

please go through this link for more details.

1 Comment

Not allowed to use BigDecimal in this project
3

It seems like you are doing some currency calculation, so always use BigDecimal

BigDecimal balStr = new BigDecimal(String.replaceAll(" ",".");

1 Comment

Good idea to use BigDecimal for precision calculations
0
String s="891692 06";
Double.parseDouble(s.replace(" ", ".")); would do the trick

You can use NumberFormatClass from the API to get the monetary information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.