Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Format Integer With Commas, No Decimal Places and Nothing for 0 Values

Okay before I get right into my question does anyone know if there is a tool for building string formats? What I'm thinking is something like a pretty simple user interface where you choose whether you want to display commas, zero values, dollar signs etc and then it spits out the stringformat for you. If there is one I'd love to know about it. If there's not it's the sort of thing people like me would love!

My question is. What is the string format for displaying an integer with comma separators for thousands, no decimal places and nothing for zero values.

I know the string format for an integer with comma separators and no decimal places is:

StringFormat='0,0.'

And I know the string format for displaying nothing for zero values is:

StringFormat='{}{0:#}'

But combining the two has me stumped. Thanks very much!

like image 570
I am Bish Avatar asked Sep 17 '13 07:09

I am Bish


People also ask

What is 0 format?

The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string.

What is string format in Java?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Syntax: There is two types of string format() method.

How do you format a float?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

Does string format round Java?

The String. format() method is typically to format a string in Java. It can also be used for rounding a double number to 2 decimal places.


1 Answers

I'm not 100% sure what you're after, but after comparing your two string formats, I think I know what you're after... please let me know if I am mistaken.

Once again, you almost had what I think you want... how about trying this:

StringFormat='{}{0:#,#.}'

Or just

StringFormat='#,#.'  (Just replace the '0' from your example with '#')

These are equivalent. Please note that again, these will both round the number to the nearest integer.

UPDATE >>>

Here are two very useful links to help you with your string.Formats in the future:

Custom Numeric Format Strings

Custom Date and Time Format Strings

like image 165
Sheridan Avatar answered Sep 17 '22 11:09

Sheridan