# Need help!!!

• 10-18-2009, 06:27 AM
ricky
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
• 10-18-2009, 06:32 AM
Fubarable
What have you got so far?

Much luck with this.
• 10-18-2009, 06:39 AM
ricky
This is what i got so far...

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%)
• 10-18-2009, 06:54 AM
Fubarable
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?
• 10-18-2009, 07:22 AM
ricky
How do i use the replace mothod to replace("-", "") inside a function?
• 10-18-2009, 07:26 AM
Fubarable
Quote:

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?
• 10-18-2009, 07:38 AM
ricky
it supposed to return a double!
• 10-18-2009, 07:44 AM
Fubarable
If it were to return a String though, I'd do it like so:
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;   }```
• 10-18-2009, 07:47 AM
Fubarable
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"
• 10-18-2009, 07:58 AM
ricky
i'll keep that in mind.thanks
• 10-18-2009, 08:20 AM
pbrockway2
Quote:

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:

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);     } }```
• 10-19-2009, 03:30 AM
jimmy-lin
Quote:

Originally Posted by ricky
This is what i got so far...

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
Code:

``` double result = 0;         System.out.printf("\$ %,(.2f", amount);           return result;```
• 10-19-2009, 03:32 AM
Fubarable
Quote:

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
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.