Results 1 to 15 of 15
Thread: decimal formatting strings
- 09-12-2011, 06:57 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
decimal formatting strings
I have inherited some code which has format strings for DecimalFormat
Some of the use #0 in front of the decimal point, which confuses me somewhat.
as far as I can see, putting 0. means "print as many digits before the decimal place as my value happens to have, unless it's<1.0, in which case print 1"
So what possible use is adding more # characters - aren't they implied???
TVM
David
- 09-13-2011, 02:02 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Re: decimal formatting strings
try to print two different format for you to see the result. One DecimalFormat is '#,##0.00' and the other one is '0,000.00'
- 09-13-2011, 02:13 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
Re: decimal formatting strings
I cant see any difference myself. I've done a few tests and read through the api and they appear to be synonymous. This is always the fun you get when inheriting code. I wouldn't get too hung up on why the wrote it like they did. If more than one person wrote the code then that may give a reason for the difference. Sometimes people will add surplus code like that simply because they think it's clearer to read.
----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 09-13-2011, 09:28 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: decimal formatting strings
produces:Java Code:public static void main(String[] args) { DecimalFormat df1 = new DecimalFormat("#,##0.00"); DecimalFormat df2 = new DecimalFormat("0,000.00"); double d = 12.1; System.out.println("df1 = " + df1.format(d)); System.out.println("df2 = " + df2.format(d)); }
df1 = 12.10
df2 = 0,012.10
Hardly synonymous.
- 09-13-2011, 11:10 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Re: decimal formatting strings
Thanks for the replies, guys.
Biggest problem is I couldn't find a decent comprehensive spec for these strings on the web ... there are a lot of tables showing the meaning of individual characters, but little about the effect of combinations of them.
My set-up is a bit complex, using eclipse to run on a remote server, and writing 5-liners isn't as easy as it should be!
I was hoping to find an interactive web page, where you put in a format string and a value, but came up empty.
------------------------------------------
The example is great, but much more complex than what I'm looking at
the code includes stuff like "#0.0###"
I'm quite clear about the stuff after the dec pt.
What puzzled me is whether the pound at the beginning could ever DO anything.
I did wonder overnight whether it would be replaced by a leading space, but clearly not.
------------------
So pardon my ignorance, but if I want something which can handle fixed field lengths, and will barf if I try to print 10000 in a 4-position field, what are my options?
David
- 09-13-2011, 11:24 AM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Re: decimal formatting strings
I am not sure if I understand you correctly but are you trying to allow the user to input upto four(4) decimal?
So when user
input: 100
output is : 100.0
and when user
input: 100.2369
output is same which is: 100.2369
You do not want to round it into something like 100.2
Is that what you are trying to do?
EDT:
Have you tried to experiments on the formats? and input different value to see which one gives you the output that you want?Last edited by mine0926; 09-13-2011 at 11:32 AM.
- 09-13-2011, 11:28 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
Re: decimal formatting strings
Bookmark this link; it's all in there.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-13-2011, 11:58 AM #8
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
- 09-13-2011, 07:49 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
Re: decimal formatting strings
As I mentioned in my previous post, there is no difference in the scenario you're describing.
#0.0### Appears to format (output) the same way as 0.0###
I haven't tested parsing (input).
Joash, the OP has already mentioned documentation containing tables showing character meanings but not found them helpful. That reasonably describes this. And he is right, it does not easily answer his question!
There is a hint (but not a definitive answer) in that leading '#' characters don't affect anything for exponential numbers (1.234E5) but the doc is silent on whether it applies to other number formats.
If I've missed something, feel free to quote the manual.----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 09-14-2011, 09:36 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: decimal formatting strings
# is used to say "these are optional numbers".
It is (as shown) pointless if you aren't attempting to show what separators to use (and where), or using the scientific notation stuff.
"Digit, zero shows as absent".
It's pretty clear.
- 09-14-2011, 09:45 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,425
- Blog Entries
- 7
- Rep Power
- 17
Re: decimal formatting strings
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-15-2011, 12:04 PM #12
Member
- Join Date
- Nov 2010
- Posts
- 54
- Rep Power
- 0
Re: decimal formatting strings
Wasn't that the point of the OP's question?Symbol; Location; Localized?; Meaning;
#; Number; Yes; Digit, zero shows as absent
The answer that has been provided to the OP's question by this forum is basically summed up as:
"#" has no effect when used on its own at the start of a format (eg: "#0.00" vs just "0.00"), because the number of digits is automatically variable anyway.
The OP's question was valid even after reading the manual sice he's seen code where (in the same code) both "#0.00" and "0.00" are used. When you see something in code like this, it can imply the person who wrote it knew something you don't. This is more than enough to suspect that "#0.00" and "0.00" could represent different decimal formats. The manual doesn't explain a difference and doesn't state they are the same (unless I've missed something). The simple inference in the manual that they are the same is not enough to answer a question such as "why does someone else think they are different" and so the manual can not be used to directly answer the OP's question.Last edited by couling; 09-15-2011 at 12:07 PM.
----Signature ----
Please use [CODE] tags and indent correctly. It really helps when reading your code.
- 09-15-2011, 12:20 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: decimal formatting strings
Of course it can.
The answer to "why does someone else think they are different", from the documentation, is "they are mistaken".
- 09-15-2011, 05:05 PM #14
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
Re: decimal formatting strings
I think the only ones who will think they are different are those non-programmers OR those who misunderstood the use of "#" and "0" in formatting numbersThis is more than enough to suspect that "#0.00" and "0.00" could represent different decimal formats.
- 09-20-2011, 12:15 PM #15
Member
- Join Date
- Sep 2011
- Posts
- 4
- Rep Power
- 0
Re: decimal formatting strings
"Digit, zero shows as absent".
It's pretty clear.
Well yeah, it is. But my "previous programmer" clearly thought otherwise, and I wasn't quite arrogant enough not to be curious.
There was the vague possibility that leading # would turn into leading space... and that was documented somewhere I hadn't looked.
Thanks all.
David
Similar Threads
-
anyone know's how to program a conversion of binary-decimal , decimal-binary
By irnie1994 in forum JCreatorReplies: 5Last Post: 08-25-2011, 07:32 PM -
Decimal to binary, octal to decimal
By matejm1994 in forum New To JavaReplies: 3Last Post: 12-26-2010, 09:59 AM -
Formatting Strings in Java
By ladykrimson in forum New To JavaReplies: 3Last Post: 11-03-2010, 08:31 AM -
formatting..
By sireesha in forum New To JavaReplies: 16Last Post: 06-26-2009, 07:11 PM -
get percentage formatting to display 2 decimal places
By gotenks05 in forum New To JavaReplies: 5Last Post: 03-03-2009, 04:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks