1. Member
Join Date
Oct 2009
Posts
8
Rep Power
0

## Need help!!!

Given a number x.

how do i put that number in () if it was negative.

for example: x = -725.21 =>> result = (725.21)

thanks

2. Member
Join Date
Oct 2009
Posts
8
Rep Power
0
This is what i got so far...

Java Code:
``` public double printCurrencyStyle(double amount)
{
double result = 0;
System.out.printf("\$ %,.2f", amount);
if (amount < 0)
System.out.printf("\$ %,.2f", + (amount));

return result;
}```

and heres the result...

Testing method printCurrencyStyle in student class MidtermProblems
--------------------------------------------------------------------------------
X printCurrencyStyle(-795.152)-> expected:<\$ [(795.15)]> but was:<\$ [-795.15]>
--------------------------------------------------------------------------------
9/10 tests passing (90%)
Last edited by Fubarable; 10-18-2009 at 06:52 AM. Reason: code tags added for readability

3. Should the method return a String representation of the number as it is to be displayed (i.e., with parenthesis if < 0) rather than return a double?

4. Member
Join Date
Oct 2009
Posts
8
Rep Power
0
How do i use the replace mothod to replace("-", "") inside a function?

5. How do i use the replace mothod to replace("-", "") inside a function?
By changing the number from a negative to a positive.

But please answer my question that I asked previously: is the method in fact supposed to return a String representation of the number and not a double as it is currently written?

6. Member
Join Date
Oct 2009
Posts
8
Rep Power
0
it supposed to return a double!

7. If it were to return a String though, I'd do it like so:
Java Code:
```   public static String printCurrencyStyle(double amount)
{
NumberFormat currFormat = NumberFormat.getCurrencyInstance();
String result = "";
if (amount < 0) {
result = "(" + currFormat.format(-amount) + ")";
} else {
result = currFormat.format(amount);
}
return result;
}```

8. I do have to mention that you should consider improving your thread title if you post here again. You want the thread title to be helpful to us, to tell us extremely briefly what your current problem is. Since 99% of folks who start threads here "need help", this title isn't helpful. A better one would be "How to print negative numbers in parenthesis"

9. Member
Join Date
Oct 2009
Posts
8
Rep Power
0
i'll keep that in mind.thanks

10. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
X printCurrencyStyle(-795.152)-> expected:<\$ [(795.15)]> but was:<\$ [-795.15]>

I'm not sure I exactly understand this. Where is the \$ supposed to come? If it's outside the number you can use:

Java Code:
```public class FormatEg {

public static void main(String[] args) {
System.out.println(printCurrencyStyle(-42d));
System.out.println(printCurrencyStyle(0d));
System.out.println(printCurrencyStyle(100.666));
}

static String printCurrencyStyle(Double d) {
return String.format("\$%(.2f", d);
}
}```

11. Member
Join Date
Oct 2009
Posts
17
Rep Power
0
Originally Posted by ricky
This is what i got so far...

Java Code:
``` public double printCurrencyStyle(double amount)
{
double result = 0;
System.out.printf("\$ %,.2f", amount);
if (amount < 0)
System.out.printf("\$ %,.2f", + (amount));

return result;
}```
You don't need to do the if to get the negative numbers, all you need to do is place the parenthecy before the decimal of 2.

like this
Java Code:
``` double result = 0;
System.out.printf("\$ %,(.2f", amount);

return result;```

12. Originally Posted by jimmy-lin
You don't need to do the if to get the negative numbers, all you need to do is place the parenthecy before the decimal of 2.

like this
Java Code:
``` double result = 0;
System.out.printf("\$ %,(.2f", amount);

return result;```
Both your code and his always returns a 0 result. A funky method indeed.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•