• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

%.2f\n

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I want to make this whole program go to only two decimal points. I got it to do it on one method, but now I want the whole program to do it, any suggestions?

 
Marshal
Posts: 66189
250
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just this once, I'll correct your code tags: the [/code] part goes after the code. Doesn't it look better now

Danielle Beard wrote:. . . make this whole program go to only two decimal points.

Not possible, I am afraid. A double works at a certain level of imprecision defined by the IEEE754 standard, and that's that. There are two things you can do:-
  • 1: Use a BigDecimal and a MathContext object to round all the calculations to 2DP; that will actually work in decimal places.
  • 2: What you are probably doing viz. displaying the result rounded to 2DP. That only rounds the display, not the calculation.
  • I would do something like this:If you print 123.45 that is six spaces, with 2 after the decimal point: %6.2f. Don't use \n which isn't the correct line end for Windows®: write %n after printf. Obviously you will have to use System.out.printf("...%6.2f%n",...); or similar every time you want to format numbers like that.

    I am afraid there is lots to say about the rest of your program: watch this space.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Link about code tags.

    Only use // comments for something short. Use /* comments */ for anything of any length; for the class or method descriptions use /** comments */
    Beard4 is a poor name for a class. AveragePriceDemo might be better. You can doubtless think of a better name still.
    Don't make any of your variables public. The only fields that are allowed public access are global constants:-Don't write // comments which don't tell you anything that isn't obvious from reading the code (e.g. lines 13, 25, and 99).
    Never write == false or == true which are both poor stye and error‑prone. We often see = written  by mistake, which is a special offer: two errors for the price of one!
    Never if (b == true)...
    Always if (b)...
    Never if (b == false)...
    Always if (!b)...
    You don't need the else if in line 82; a plain simple else will work. Lines 71‑77 are duplicated in lines 85‑92. I think you only need an if to calculate the tax and you can then use only one set of print statements.
    You still have some formatting problems, e.g. too many empty lines, no spaces after commas, inconsistent placement of {}s (look at lines 15, 37, 42 and 99, which are all different from one another).
    If you only use printf in a few places, you will only get your desired formatting for the output from those lines.
     
    Saloon Keeper
    Posts: 21248
    137
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:
    You still have some formatting problems, e.g. too many empty lines



    While Campbell's precise æsthetics might not exactly match mine, I have no problem with putting blank likes between sections of code to set them off. Multiple blank line sequences are overkill, though. They also make it harder for everything to fit on a terminal screen at the same time, which can make it harder to see what's happening.

    Also, use the JavaDoc style commenting form! It not only makes good documentation, you can run the JavaDoc utility and get your own private documentation package. Examples:





    Just as a side note, it's generally better/more flexible if you ask for the values in a separate method, then pass them as arguments to the averaging method. Which, incidentally, would probably be better named "computeAverage". Using "get" as the prefix of a method in Java can confuse people because it's the standard for property access methods, and you're not accessing a property value, you're calculating.
     
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Danielle Beard wrote:So, I want to make this whole program go to only two decimal points. I got it to do it on one method, but now I want the whole program to do it, any suggestions?


    To answer your main question... If you're thinking about minimizing the tedium of having to write System.out.printf(".... $%.2f%n", someAmount); over and over again then extract that work to a private method and call that method instead.

    Something like this:

    Example usage:

     
    Bartender
    Posts: 3606
    151
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can even add the parameters int length and int decimalPlaces to fmt(...), to give it more flexibility.

    And if you get fed up having to type 'System.out.printf' then just type 'out.printf' and your IDE will do a static import 'System.out' for you.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Piet Souris wrote:. . . add the parameters int length and int decimalPlaces to fmt(...), to give it more flexibility. . . .

    Good idea, Piet, but maybe you would like to show DB an example of how that works.
     
    Piet Souris
    Bartender
    Posts: 3606
    151
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I used:

    that you could use like:

    and if you have a program full of fmt(x, 10, 2) you could make a default fmt, such as

    Edit: I myself found such a default method very handy when printing out a 2D array (or: array of arrays), to get the columns neatly aligned.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Why not pass the first and last parts of the format String as arguments too? I think your fmtdef() method might better be called format10and2().The Unicode escape \u2019 is ’ (apostrophe/right quote).
     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The cost of flexibility is complexity. If your intent is just to relieve the tedium of having to write "$ %.2f" all over the place and then (forgetting that most IDEs have a global Search-Replace feature) having to change all those instances if you decide to change the format, then I'd start with The Simplest Thing That Could Possibly Work.  Only when I find I need more flexibility would I bother to go ahead and build that flexibility in.
     
    Piet Souris
    Bartender
    Posts: 3606
    151
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good points. Well, OP has seen some possibilities, and that is never a bad thing.
     
    Tim Holloway
    Saloon Keeper
    Posts: 21248
    137
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anndddd....

    One last thing. Note that the whole point here is to format a currency print. Don't forget that Java I18N has its own ways of dealing with that - including allowing for currencies where fractions are marked with commas, more than 2 decimal places are used, and alternative separators are used for very large numbers.
     
    Junilu Lacar
    Marshal
    Posts: 14336
    237
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Holloway wrote:Anndddd....

    One last thing. Note that the whole point here is to format a currency print. Don't forget that Java I18N has its own ways of dealing with that - including allowing for currencies where fractions are marked with commas, more than 2 decimal places are used, and alternative separators are used for very large numbers.


    To that point, by extracting the work to a single method, you'd only need to make the change in that one place if you decided to go with the approach Tim is suggesting.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:The cost of flexibility is complexity. . . .

    That is why I resisted the temptation to give that method even more parameters,, e.g. a 0 before the width. That would have produced totally incomprehensible method headings.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Holloway wrote:. . . format a currency . . . Java I18N has its own ways of dealing with that . . .

    Doesn't printf() do something similar by using the default Locale?
     
    Tim Holloway
    Saloon Keeper
    Posts: 21248
    137
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Tim Holloway wrote:. . . format a currency . . . Java I18N has its own ways of dealing with that . . .

    Doesn't printf() do something similar by using the default Locale?



    Not for currency. There isn't a printf format token specifically for currency. The printf method might understand the local difference between commas and periods. Although that would be exceeding the remit of the original C function whence it came. As would putting large-number grouping characters in.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Holloway wrote:. . . Not for currency. . .

    Thank you.

    The printf method might understand the local difference between commas and periods.

    Not tried it recently, but I think it does use Locale‑specific radix points.

    Campbell's JShell wrote:jshell> Locale.setDefault(Locale.GERMANY);

    jshell> System.out.printf("%10.2f%n", 1234.56);
      1234,56

    I think that shows it working.

    . . . exceeding the remit of the original C function . . .

    What's the remit of the original C printf() got to do with it?
     
    Tim Holloway
    Saloon Keeper
    Posts: 21248
    137
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:What's the remit of the original C printf() got to do with it?



    Well. if you're going to nick some other platform's function by name and characteristics, it's expected that you'll be providing the same functionality. Any surprises may be nice, but they could come back and bite as well.

    But it's good to know about the decimal marker.
     
    Campbell Ritchie
    Marshal
    Posts: 66189
    250
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sounds like one of those things we agree to differ about.
     
    Did you just should on me? You should read this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!