Results 1 to 13 of 13

Thread: Need help!!!

  1. #1
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    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 07:52 AM. Reason: code tags added for readability

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  5. #5
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    How do i use the replace mothod to replace("-", "") inside a function?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  7. #7
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    it supposed to return a double!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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;
       }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #10
    ricky is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    i'll keep that in mind.thanks

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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);
        }
    }

  12. #12
    jimmy-lin is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by ricky View Post
    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;

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jimmy-lin View Post
    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
  •