COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Number padding... (trailing zeros'.)

• Email
• Follow

```Hi

Is there a nice way of formatting a decimal to have a fixed number of
trailing zero's?
I am decimalising some data to feed into another application. The input
requires 6 decimal places.
Using sprintf I can apply various formats, like leading zero's but can't
figure out how to add trailers...

For most of my data it's not a problem, but I have a couple of oddities that
divide neatly leading me with 4 decimal places.
One example I have. After conversion I get: 0.1029  but I want 0.102900.

At the moment I am multiplying my variable by 100000 and doing a substr on
the leading 6 characters, but there must be a nicer way than this...

Any help would be much appreciated.

Pete

```
 0
Reply ThePotPlants 5/23/2004 7:24:11 AM

See related articles to this posting

```On Sun, 23 May 2004 19:24:11 +1200, ThePotPlants wrote:

> Hi
>
> Is there a nice way of formatting a decimal to have a fixed number of
> trailing zero's?
> I am decimalising some data to feed into another application. The input
> requires 6 decimal places.
> Using sprintf I can apply various formats, like leading zero's but can't
> figure out how to add trailers...

\$ perl -le 'printf "%.6f", 0.1029'
0.102900

Dave...

```
 0
Reply Dave 5/23/2004 7:32:03 AM

```"ThePotPlants" <thepotplants@yahoo.com> wrote:

: Is there a nice way of formatting a decimal to have a fixed number of
: trailing zero's?

Yes.

: One example I have. After conversion I get: 0.1029  but I want 0.102900.

printf '%.6f', 0.1029

```
 0
Reply tiltonj 5/23/2004 7:33:15 AM

```ThePotPlants wrote:
> Is there a nice way of formatting a decimal to have a fixed number of
> trailing zero's?
> I am decimalising some data to feed into another application. The
> input requires 6 decimal places.
> Using sprintf I can apply various formats, like leading zero's but
> can't figure out how to add trailers...

Hmmm, strange.
Doesn't the second example on the sprintf perldoc page work for?

jue

```
 0
Reply J 5/23/2004 7:37:57 AM

```"Dave Cross" <dave@dave.org.uk> wrote in message
news:pan.2004.05.23.07.32.03.598268@dave.org.uk...
> On Sun, 23 May 2004 19:24:11 +1200, ThePotPlants wrote:
>
> > Hi
> >
> > Is there a nice way of formatting a decimal to have a fixed number of
> > trailing zero's?
> \$ perl -le 'printf "%.6f", 0.1029'
> 0.102900
>
> Dave...

Thanks guys :)

```
 0
Reply ThePotPlants 5/23/2004 10:19:26 PM

4 Replies
875 Views

Similar Articles

12/6/2013 9:10:28 AM
[PageSpeed]

 Reply:

Similar Artilces:

Find a distance between two zip codes
anyone knows the formula for Finding a distance between two zip codes? Joe On 17 Jul 2004 20:55:05 GMT, Galsaba wrote: > anyone knows the formula for Finding a distance between two zip codes? Do you even know the locations? The geometry from latitude and longtitude is fairly straighforward. More difficult if you want a 'distance by road' (in fact, that is where the problem gets very interesting, and it is what I suspect your homework is actually asking for) At this stage, given the level of your question, you should be asking it on c.l.j.help. <http://www.physci.org/code

Removing trailing zeros from java.math.BigDecimal produces infinite loop
[code] /** * Trim all trailing zeros found after the decimal point of {@link java.math.BigDecimal} * @param n {@link java.math.BigDecimal} * @return n {@link java.math.BigDecimal} * <a href="http://groups.google.com/group/ comp.lang.java.programmer/browse_frm/thread/5ad5a973fcbf442b/ 52012c87f1051dd5?lnk=st&q=removing+trailing+zeros +java&rnum=2&hl=en#52012c87f1051dd5">more information</a> */ public static BigDecimal trim(BigDecimal n) { try { while (true) { n = n.setScale(n.scale() - 1); } } catch (ArithmeticException e) {} // DO NOTHING NO MORE TRAILING ZEROS FOUND return n; } [/code] This method is supposed to remove any and all trailing zeroes from a given java.math.BigDecimal parameter n and return it "trimmed". However, if n < 1 or n > -1 the method goes into an infinite loop. Not sure how to patch this method to prevent this from occurring from this condition. What tips do you have to prevent this from occurring? I thought of BigDecimal.ROUND_HALF_UP, but to no avail. Thanx Phil phillip.s.powell@gmail.com wrote