Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

String.format right-justified with '$' symbol and comma seperation

 
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to format some variables so that there are 2 columns. First is the title which is left justified, second is either strictly an integer or a dollar value. Both the integers and dollar values should be right justified and every 3 numbers separated with commas. The Dollar values only should have the dollar symbol. Ive managed to do this all except for the right justification, most of what i have tried doesn't work with different sized numbers or changes the format entirely

 
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
G Atwal
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this initially but with different lengths of integers the format isn't maintained.
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gurtej Grewal wrote:I tried this initially but with different lengths of integers the format isn't maintained.

Can you cut and paste the output you're getting here? Please enclose in Code tags to preserve formatting.
 
G Atwal
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the Code Below The Input/Output Is:


Carey Brown wrote:

 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try these sizes. The first number is the TOTAL length including decimal point, decimal places, and any quantity of commas.
 
G Atwal
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With this, the number is always on the right which is good, the only thing is that the $ symbol always has a gap between itself and the number
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I'm not happy with the dangling '$' myself. Two ways to handle that: 1) put the '$' on the right side, or 2) do the formatting in two steps.

 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Personally I think there's a certain elegance to choice '1' but people are so used  to seeing the '$' to the left of the number that their brain seizes up when it's not there.

Post your latest output when you get a chance, I'd like to see how it looks.
 
G Atwal
Ranch Hand
Posts: 63
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final output using the second method:
 
Carey Brown
Saloon Keeper
Posts: 8920
76
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good. Don't forget to verify the max widths you've chosen with some worst case test scenarios.
 
G Atwal
Ranch Hand
Posts: 63
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried it with inputs 1, 10, 10_000, 100_000_000, 2_147_483_647 and it seems to be working perfectly
 
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

G Atwal wrote:. . . and every 3 numbers separated with commas.

That is a locale specific feature; the comma tag produces a comma as a thousands separator in Britain, North America, Australia, etc. In most of the rest of Europe it will use a dot. In India, it may put the thousands separator in different places.

. . . dollar symbol. . . .

Formatter doesn't seem to have a currency symbol flag. It might be necessary to use the old way to format numbers, which you can find in the Java™ Tutorials (link 1 and link 2): NumberFormat#getCurrencyInstance().
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . In India, it may put the thousands separator in different places. . . .

I tried it and it didn't.
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

MyJShell wrote:jshell> NumberFormat format = NumberFormat.getCurrencyInstance(Locale.US);
format ==> java.text.DecimalFormat@67500

jshell> format.format(1234567.89);
$20 ==> "$1,234,567.89"

I can't remember when I last used NumberFormat rather than printf() and similar.
 
Bartender
Posts: 4732
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another idea:

if you have a record Atwal(String title, double value, boolean isDollarAmount) with a method 'getWidthOfFormattedValue', then if you have a List of these records, you can calculate a tailor-made format for all of them. Then you do not need fixed-lengths.
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good idea, Piet. You can also change the datatype of the record from double to what you should use for money. You can also override toString() to include the $ sign.
Or you can use inheritanceIf you are feeling ambitious you can reduce the potential baleful effects of inheritance by making the subclasses static nested classes.Nested classes and their enclosing types have access to each other's private constructors and members. The private constructors restrict instantiation to within the same class (=enclosing type). That means you can use a non‑private faactory method to make instance avaiable to other code. Remember that factory methods are usually static because they may have to be called beforee any instances are available.
You can improve your design by having a subtype called CurrencyRecord with a currency field.
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use == true nor == false which are not good style and very error‑prone. We often see people write = true or = false by mistake.
Never if (b == true) ... nor if (b == false) ... please.
Always if (b) ... or if (!b) ... please.
 
Piet Souris
Bartender
Posts: 4732
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, overwhelming ideas!

I meant the record of java 17, just for ease of use. I would give it a valueToString method, something like:

and

And lastly I had the idea of a List of these records, so that you could do


Now it is easy to come up with a format that fits all. Only question I have is: should the $-signs be aligned?

But I am typing this out of my head, so see this only as an idea,
 
Campbell Ritchie
Marshal
Posts: 74637
335
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:Wow, overwhelming ideas!

Thank you

. . . should the $-signs be aligned? . . .

I thought the priginal problem was that OP wanted the $ signs not to align.
I think I would still go for the plain simple toString() method. That means the formatting of a Record object (sorry for misreading the name) is done by the object itself, which I would prefer as an object‑oriented solution.
Yes, you can implement that as a record, but records are implicitly final classes, so my idea about nested classes wouldn't work at all well.
 
Piet Souris
Bartender
Posts: 4732
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some food for OP to digest!    
 
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, you can implement that as a record, but records are implicitly final classes, so my idea about nested classes wouldn't work at all well.


You can still do that, only without toString:
The only difference is that you have to call stringValue() explicitly.
reply
    Bookmark Topic Watch Topic
  • New Topic