• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add a zero for decimal part  RSS feed

 
Cascada Britt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm having a problem with this exercise. This is the prompt:

Add the following method to the BankAccount class:
public String toString()
Your method should return a string that contains the account's name and balance separated by a comma and space. For example, if an account object named benben has the name "Benson" and a balance of 17.25, the call of benben.toString() should return:
Benson, $17.25
There are some special cases you should handle. If the balance is negative, put the - sign before the dollar sign. Also, always display the cents as a two-digit number. For example, if the same object had a balance of -17.5, your method should return:
Benson, -$17.50

Here is my code:

public String toString() {
String result = name + ", ";
if (balance < 0) {
result += "-";
}
return result += "$" + Math.abs(balance);
}

My code only works in case there are full two numbers for the cents part, not for the case when there's only one number. So I wonder how I can add an extra zero when needed. I'm thinking about somehow I can get only the decimal part and add a zero if it's less than 10, but I don't know how I can extract just the decimal part from the number. (The balance is just a double and it doesn't have any separate field for dollars and cents)
Thanks!
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may like to check this oracle documentaton for the formatting.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see no point in checking whether the number is negative and prepending "-" to its absolute value if it is negative.
Printing a negative number will always print a minus sign for you.

This might be overcomplicating things, but you should be careful with locales.
For example, for English locale this line:prints:
1.32

For Polish locale, it prints:
1,32

Your requirement is a dot is always used as a decimal separator.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

How far did you get with the formatting link TG posted? I would use the percent tags. Note the String#format method which uses the percent tags.
 
Mahesh Jain P
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public String toString(){
String result = name + ", ";
if (balance < 0){
result += "-"+"$" + String.format("%.2f",Math.abs(balance));
}
else{
result += "$" + String.format("%.2f",balance);
}
return result;
}
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is a much simpler way to do that, as people mentioned last year.
 
Mahesh Jain P
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

//yes this is it
public String toString(){
        String result = name + ", ";
        if (balance < 0)
            result += "-";
        return result+= "$" + String.format("%.2f",Math.abs(balance));
}
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a still simpler way to do it. And don't write your own minus.
 
Mahesh Jain P
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is any simpler way to code, can you code it.. or else ^_^
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not add your own minus sign; that is done automatically by all the printing methods. Nor should you use the abs method. I have changed the %f tag slightly. Always us the @Override annotation if you are overriding a method.

I didn't notice that you have to put the minus sign before the dollar sign. If that is required, try this, which will require the abs method after all:-The 6 may be omitted.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not use floating‑point arithmetic for money; you should always use BigDecimal Now your toString method would become this:-[edit]Add abs call which I had forgotten[/edit]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!