From: Roedy Green on 28 Jul 2010 22:23 If you wanted to trim trailing 0s from a string representation of a number after the decimal point, can you do it directly with DecimalFormat, or do you need to do String operations on the result? -- Roedy Green Canadian Mind Products http://mindprod.com You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
From: John B. Matthews on 29 Jul 2010 00:08 In article <8ep1565th6q070srgj715t6j7rc5rfuffe(a)4ax.com>, Roedy Green <see_website(a)mindprod.com.invalid> wrote: > If you wanted to trim trailing 0s from a string representation of a > number after the decimal point, can you do it directly with > DecimalFormat, or do you need to do String operations on the result? I'm pretty sure you'd have to convert the String to a Number, long or double before you could format() it. -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
From: Eric Sosman on 29 Jul 2010 00:22 On 7/28/2010 10:23 PM, Roedy Green wrote: > If you wanted to trim trailing 0s from a string representation of a > number after the decimal point, can you do it directly with > DecimalFormat, or do you need to do String operations on the result? If you're starting with the numeric value (as opposed to an already-generated String), just use the '#' pattern character, as NumberFormat fmt = new DecimalFormat("#.##"); This will format 1.20 as "1.2" and 3.45 as "3.45". -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Roedy Green on 29 Jul 2010 01:51 On Thu, 29 Jul 2010 00:08:23 -0400, "John B. Matthews" <nospam(a)nospam.invalid> wrote, quoted or indirectly quoted someone who said : >I'm pretty sure you'd have to convert the String to a Number, long or >double before you could format() it. I have it as a double. DecimalFormat.format gives me the String. My question is, is there any tricky pattern you can give it to trim trailing 000, or do you need to process the output of .format as a String? -- Roedy Green Canadian Mind Products http://mindprod.com You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
From: Roedy Green on 29 Jul 2010 01:52 On Thu, 29 Jul 2010 00:22:32 -0400, Eric Sosman <esosman(a)ieee-dot-org.invalid> wrote, quoted or indirectly quoted someone who said : > > NumberFormat fmt = new DecimalFormat("#.##"); > >This will format 1.20 as "1.2" and 3.45 as "3.45". Any is there a way to trim .00 to get rid of the dot too, or is that the default behaviour? -- Roedy Green Canadian Mind Products http://mindprod.com You encapsulate not just to save typing, but more importantly, to make it easy and safe to change the code later, since you then need change the logic in only one place. Without it, you might fail to change the logic in all the places it occurs.
|
Next
|
Last
Pages: 1 2 Prev: Apache Tomcat https link goes down but http works Next: Testing As Proof |