1 2 Previous Next 18 Replies Latest reply: Aug 20, 2007 5:32 AM by 807600 RSS

    Need only two decimal places, but BigDecimal isn't working

    807600
      Hello again. This time I've got a problem with getting only two decimal places. I tried using BigDecimal but when I compile it, I get an error "cannot find symbol class BigDecimal" and a "cannot find symbol variable BigDecimal".

      Grrr... I just want my value to have only 2 decimal places before it ends up in my text field.

      Any help will be appreciated.

      - Phonse
      if (ostuff1.equals("amt tendered"))
                     { 
                       finalamt = total1 * 1.12;
                       change = cash1 - finalamt;
                 BigDecimal finalamt2 = new BigDecimal(finalamt);
                    finalamt2 = finalamt2.setScale(2, BigDecimal.ROUND_DOWN);
                    BigDecimal change2 = new BigDecimal(change);
                    change2 = change2.setScale(2, BigDecimal.ROUND_DOWN);
                    finalamt = doubleValue(finalamt2);
                    change = doubleValue(change2);
                       addItem(String.format("Amount Tendered:          "+"P "+finalamt));
                       addItem(String.format("Change:                     "+"P "+change));
                     }
        • 1. Re: Need only two decimal places, but BigDecimal isn't working
          800282
          Use DecimalFormat for representing your floating point values with a specific precision.

          http://www.exampledepot.com/egs/java.text/FormatNum.html
          http://java.sun.com/j2se/1.4.2/docs/api/java/text/DecimalFormat.html
          • 2. Re: Need only two decimal places, but BigDecimal isn't working
            807600
            You don't seem to have imported java.math.BigDecimal .
            • 3. Re: Need only two decimal places, but BigDecimal isn't working
              807600
              @ sabre150

              I already imported it. I still get the same error. When I was looking up stuff on how to use BigDecimal, I already came across people using it with System.out.print which I don't use because I'm running a GUI. Now I'm having a hard time trying to use BigDecimal.

              @ prometheuzz

              I already tried that a while ago, but I'm getting even more errors using a formatter. "cannot find symbol variable formatter" and "cannot find symbol class BigDecimal" always appears. And I already imported java.math.BigDecimal.*;
              • 4. Re: Need only two decimal places, but BigDecimal isn't working
                807600
                Incidentally, you should be using the constructor of BigDecimal which takes a String. That's what determines the precision of your BigDecimal. Other than that, it really does look like you haven't imported it
                • 5. Re: Need only two decimal places, but BigDecimal isn't working
                  807600
                  Bah... I don't know what you mean by constructor. Go easy on the jargon please. I've been taking up Java only for a month now. We're taking up Java in computer class this sem.

                  How do you know I haven't imported it? I just placed 'import java.math.BigDecimal.*; ' beside the other classes I imported. But the problem remains the same whether or not I use a string or a double to be turned into a BigDecimal value.

                  Seems like a career in computers will be off my list when I eventually get to college.
                  • 6. Re: Need only two decimal places, but BigDecimal isn't working
                    800282
                    Ahh damn... I don't know what you mean by
                    constructor. Go easy on the jargon please. I've been
                    taking up Java only for a month now.
                    If you don't know what a constructor is, you really should read some basic tutorials:
                    http://java.sun.com/docs/books/tutorial/java/index.html

                    How do you know I haven't imported it? I just placed
                    'import java.math.BigDecimal.*; ' beside the other
                    classes I imported. But the problem remains the same
                    whether or not I use a string or a double to be
                    turned into a BigDecimal value.
                    Import it like this:
                    import java.math.BigDecimal;
                    // or
                    import java.math.*;
                    not
                    import java.math.BigDecimal.*;
                    • 7. Re: Need only two decimal places, but BigDecimal isn't working
                      800282
                      ...
                      Seems like a career in computers will be off my list
                      when I eventually get to college.
                      That's the spirit!
                      : |
                      • 8. Re: Need only two decimal places, but BigDecimal isn't working
                        807600
                        No wonder. I should have just imported the java.math class. Thanks prometheuzz! Finally, got the decimal places to work.
                        • 9. Re: Need only two decimal places, but BigDecimal isn't working
                          807600
                          Bah... I don't know what you mean by constructor. Go
                          easy on the jargon please.
                          Sorry, but there's really no way to avoid jargon in this field! The constructor is the thing you call when you use "new". What I meant there was
                          BigDecimal bd = new BigDecimal("20.00"); // bd is now precise to 2 decimal places
                          I've been taking up Java
                          only for a month now. We're taking up Java in
                          computer class this sem.

                          How do you know I haven't imported it? I just placed
                          'import java.math.BigDecimal.*; ' beside the other
                          classes I imported. But the problem remains the same
                          whether or not I use a string or a double to be
                          turned into a BigDecimal value.
                          I know you haven't imported it because the compiler tells you so. It doesn't lie. Looking at your code, you haven't imported it because you've tried to import a package called java.math.BigDecimal, which doesn't exist. Just use "import java.math.BigDecimal;" and it'll work
                          Seems like a career in computers will be off my list
                          when I eventually get to college.
                          Probably wise if you hate jargon. The developer's entire vocabulary could be considered "jargon"!
                          • 10. Re: Need only two decimal places, but BigDecimal isn't working
                            807600
                            No wonder. I should have just imported the java.math
                            class. Thanks prometheuzz! Finally, got the decimal
                            places to work.
                            java.math is a package, not a class. And you're better off importing individual classes rather than entire packages
                            • 11. Re: Need only two decimal places, but BigDecimal isn't working
                              807600
                              @ georgemc

                              Isn't it better to import whole packages instead so you can have everything available when you call them? I mean, it's kinda hard to import individual classes everytime you need to call them.

                              Offtopic @ prometheuzz

                              LOL. When I was a kid still playing my Playstation I wanted to have a career in computers when I got to college. Now I'm 14 and a 3rd year high school student and I just got a reality check -- computers just isn't for me. And alot of those professionals say Java is the "easiest" programming language. O_O Meh. I'll just follow my other dream of becoming a chef. Just seems like my parents won't like it... Bleh... Two more years... Two more years...
                              • 12. Re: Need only two decimal places, but BigDecimal isn't working
                                807600
                                @ georgemc

                                Isn't it better to import whole packages instead so
                                you can have everything available when you call them?
                                I mean, it's kinda hard to import individual classes
                                everytime you need to call them.
                                Nope. If your class calls a lot of other classes, you've almost certainly got a design problem. The class is too busy. A class should have a single, clear responsibility. There are a few exceptions to this, but generally, if your class uses lots of other classes, it's doing too much. Read about the "god class" anti-pattern for more on this
                                Offtopic @ prometheuzz

                                LOL. When I was a kid still playing my Playstation I
                                wanted to have a career in computers when I got to
                                college. Now I'm 14 and a 3rd year high school
                                student and I just got a reality check -- computers
                                just isn't for me. And alot of those professionals
                                say Java is the "easiest" programming language. O_O
                                Meh. I'll just follow my other dream of becoming a
                                chef. Just seems like my parents won't like it...
                                Bleh...
                                Become a plumber. People always need plumbers. I should have listened to my mother.....
                                • 13. Re: Need only two decimal places, but BigDecimal isn't working
                                  800282
                                  @ georgemc

                                  Isn't it better to import whole packages instead so
                                  you can have everything available when you call them?
                                  I mean, it's kinda hard to import individual classes
                                  everytime you need to call them.
                                  No. You can get conflicts when importing complete packages. If you import
                                  java.awt.*;
                                  java.util.*;
                                  and then declare
                                  List list;
                                  the compiler does not know which List you meant, java.util.List or java.awt.List.

                                  Offtopic @ prometheuzz

                                  LOL. When I was a kid still playing my Playstation I
                                  wanted to have a career in computers when I got to
                                  college. Now I'm 14 and a 3rd year high school
                                  student and I just got a reality check -- computers
                                  just isn't for me. And alot of those professionals
                                  say Java is the "easiest" programming language. O_O
                                  Meh. I'll just follow my other dream of becoming a
                                  chef. Just seems like my parents won't like it...
                                  Bleh... Two more years... Two more years...
                                  Sure, but if you like programming, work (hard) for it. Most people have to work hard in order to become good at something, no one is born as a programmer. This goes for becoming a chef as well.
                                  • 14. Re: Need only two decimal places, but BigDecimal isn't working
                                    807600
                                    Wait, so what if you're really planning to use the 'list' method from both classes? How will the compiler recognize which class to get it from?

                                    Off-topic:

                                    Meh... I've been cooking as a hobby for the past 3 years and I can pretty much cook decent food if my life depended on it. I suck at baking though. Never really got to practice because our over is disfunctional. Seems like all kids that got to play video games had or still has a dream to have a career in computers. I just wonder how far they'll go before they're wiper out by the competition.
                                    1 2 Previous Next