# How would you get rid of lagging zeros?

Discussion in 'Java' started by Ramon F Herrera, Aug 16, 2010.

1. ### Ramon F HerreraGuest

I need my code to convert a bunch of Strings (containing Double
numbers) to a canonical form:

"0.3750" should become "0.375"

In essence, I need to remove any extra zeros on the right side. You
may assume all the numbers have a period.

TIA,

-Ramon

Ramon F Herrera, Aug 16, 2010

2. ### Daniel PittsGuest

On 8/16/2010 3:30 PM, Ramon F Herrera wrote:
>
> I need my code to convert a bunch of Strings (containing Double
> numbers) to a canonical form:
>
> "0.3750" should become "0.375"
>
> In essence, I need to remove any extra zeros on the right side. You
> may assume all the numbers have a period.

I would use replaceAll method the the appropriate regex.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Daniel Pitts, Aug 17, 2010

3. ### Arved SandstromGuest

Ramon F Herrera wrote:
> I need my code to convert a bunch of Strings (containing Double
> numbers) to a canonical form:
>
> "0.3750" should become "0.375"
>
> In essence, I need to remove any extra zeros on the right side. You
> may assume all the numbers have a period.
>
> TIA,
>
> -Ramon

At the risk of being pedantic, you do know that 0.375 and 0.3750 are not the
same thing, right? Not even close. You can't just arbitrarily remove zeros
on the right like that; 0.375 could also legitimately be an approximation of
0.3753 or 0.3746.

I'm just wondering in what world of math is this kind of thing a "canonical
form"? I'd feel more comfortable if you'd just said you were rounding all
numbers to a precision such that no zeros remained on the right...which is
still odd.

AHS
--
Before a man speaks it is always safe to assume that he is a fool.
After he speaks, it is seldom necessary to assume it. -- H.L. Mencken

Arved Sandstrom, Aug 17, 2010
4. ### Robert TomsickGuest

Daniel Pitts wrote:

> On 8/16/2010 3:30 PM, Ramon F Herrera wrote:
>>
>> I need my code to convert a bunch of Strings (containing Double
>> numbers) to a canonical form:
>>
>> "0.3750" should become "0.375"
>>
>> In essence, I need to remove any extra zeros on the right side. You
>> may assume all the numbers have a period.

>
> I would use replaceAll method the the appropriate regex.

I haven't really tested this extensively, but wouldn't the following work:

--- code ---
String inputString = "0.3750";

String stripped = Double.valueOf(inputString).toString();
--- /code ---

Robert Tomsick, Aug 17, 2010
5. ### LewGuest

Ramon F Herrera wrote:
> I need my code to convert a bunch of Strings (containing Double
> numbers) to a canonical form:
>
> "0.3750" should become "0.375"
>
> In essence, I need to remove any extra zeros on the right side. You
> may assume all the numbers have a period.

You've been around long enough to know better, Ramon.

--
Lew

Lew, Aug 17, 2010
6. ### Ramon F HerreraGuest

On Aug 16, 7:02 pm, Lew <> wrote:
> Ramon F Herrera wrote:
> > I need my code to convert a bunch of Strings (containing Double
> > numbers) to a canonical form:

>
> > "0.3750" should become "0.375"

>
> > In essence, I need to remove any extra zeros on the right side. You
> > may assume all the numbers have a period.

>

That's cute, Lew. You are multi-posting, I am not.

The java.help NG looked like a cemetery. I could even hear the echo of
my keyboard, so I decided to try my luck here.

-RFH

Ramon F Herrera, Aug 17, 2010
7. ### Ramon F HerreraGuest

On Aug 16, 6:23 pm, "Arved Sandstrom" <> wrote:
> Ramon F Herrera wrote:
> > I need my code to convert a bunch of Strings (containing Double
> > numbers) to a canonical form:

>
> > "0.3750" should become "0.375"

>
> > In essence, I need to remove any extra zeros on the right side. You
> > may assume all the numbers have a period.

>
> > TIA,

>
> > -Ramon

>

> At the risk of being pedantic, you do know that
> 0.375 and 0.3750 are not the same thing, right?
> Not even close.

The numbers in question are the possible widths of a screw, in inches:

- 1
- 1.125
- 1.25
- 1.375
- 1.5
- 1.75
- 2
- 2.25

(those are the only possible widths).

I do not even make any calculation with those values, they are only
used as a key to several Maps. All I need is the values to be
different and to have a canonical form for each. I could use these
"values" as keys:

- "one inch"
- "one inch and a quarter"

etc.

Thanks, and I always use the "pedantic" flag, so your point is well
taken!

-Ramon

Ramon F Herrera, Aug 17, 2010
8. ### Arved SandstromGuest

Ramon F Herrera wrote:
> On Aug 16, 6:23 pm, "Arved Sandstrom" <> wrote:
>> Ramon F Herrera wrote:
>>> I need my code to convert a bunch of Strings (containing Double
>>> numbers) to a canonical form:

>>
>>> "0.3750" should become "0.375"

>>
>>> In essence, I need to remove any extra zeros on the right side. You
>>> may assume all the numbers have a period.

>>
>>> TIA,

>>
>>> -Ramon

>>

>
> > At the risk of being pedantic, you do know that
> > 0.375 and 0.3750 are not the same thing, right?
> > Not even close.

>
>
> Thanks for your concern, Arved.
>
> The numbers in question are the possible widths of a screw, in inches:
>
> - 1
> - 1.125
> - 1.25
> - 1.375
> - 1.5
> - 1.75
> - 2
> - 2.25
>
> (those are the only possible widths).
>
> I do not even make any calculation with those values, they are only
> used as a key to several Maps. All I need is the values to be
> different and to have a canonical form for each. I could use these
> "values" as keys:
>
> - "one inch"
> - "one inch and a quarter"
>
> etc.
>
> Thanks, and I always use the "pedantic" flag, so your point is well
> taken!
>
> -Ramon

Good explanation, one which did not even occur to me.

Don't mind me; my background is physics and scientific programming, so I am

AHS
--
Before a man speaks it is always safe to assume that he is a fool.
After he speaks, it is seldom necessary to assume it. -- H.L. Mencken

Arved Sandstrom, Aug 17, 2010
9. ### Stefan RamGuest

Ramon F Herrera <> writes:
>"0.3750" should become "0.375"

Strings are immutable.

However, you can create a new string from a string
(or change a StringBuffer oder StringBuilder).

An algorithm in pseudocode assuming zero-based indices:

let l := the length of the string;
let last := l - 1;
while the character at position »last« in the string is "0"
do
last := last - 1
endwhile;
return substring( source: string, start: 0, end: last );

Stefan Ram, Aug 17, 2010
10. ### LewGuest

Lew wrote:

Ramon F Herrera wrote:
> That's cute, Lew. You are multi-posting, I am not.

You posted to two separate newsgroups independently. That's multi-posting.
It breaks the thread - for example, in clj.programmer you can't see my
response to your question. What I'm doing is cross-posting - putting more
than one newsgroup in the destination header simultaneously. That way all
answers in either forum chain to the correct conversation.

I'm trying to be helpful, not cute.

--
Lew

Lew, Aug 17, 2010
11. ### LewGuest

Ramon F Herrera wrote:
> The numbers in question are the possible widths of a screw, in inches:
>
> - 1
> - 1.125
> - 1.25
> - 1.375
> - 1.5
> - 1.75
> - 2
> - 2.25
>
> (those are the only possible widths).

Those can be modeled as class instances, e.g., Double() or an enum. Enums can
have a custom 'toString()' and static 'fromString()' method - such a useful
idea I built it into my Eclipse and NetBeans code templates.

> I do not even make any calculation with those values, they are only
> used as a key to several Maps. All I need is the values to be
> different and to have a canonical form for each. I could use these
> "values" as keys:
>
> - "one inch"
> - "one inch and a quarter"
>
> etc.

<untested>
/* ScrewSize
* \$RCSfile\$
*/
package com.lewscanon.util;

import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;

/** ScrewSize.
* 1, 1.125, 1.25, 1.375, 1.5, 1.75, 2, 2.25
*/
public enum ScrewSize
{
ONE_INCH( 1.0, "one inch inches" ),
ONE_AND_ONE_EIGHTH( 1.125, "one inch and an eighth" ),
ONE_AND_ONE_QUARTER( 1.25, "one inch and a quarter" ),
ONE_AND_THREE_EIGHTHS( 1.375, "one and three eighths inches" ),
ONE_AND_ONE_HALF( 1.5, "one and a half inches" ),
ONE_AND_THREE_QUARTERS( 1.75, "one and three quarters inches" ),
TWO_INCHES( 2.0, "two inches" ),
TWO_AND_ONE_QUARTER( 2.25, "two and one quarter inches" ),
;
private static final long serialVersionUID = 1L;

private final Double ssize;
private final String repr;

/**
* Constructor - package-private.
* @param ssiz Double ssize.
* @param rep String representation of enum value.
*/
ScrewSize( Double ssiz, String rep )
{
this.ssize = ssiz;
this.repr = rep;
}

/**
* Get the {@code Double} ssize.
* @return Double ssize.
*/
public Double getSsize()
{
return this.ssize;
}

/**
* Get the {@code String} representation.
* @return String representation.
*/
@Override
public String toString()
{
return this.repr;
}

/**
* Look up enum constant from String representation.
* @param rep String representation of enum value.
* @return ScrewSize constant matching the representation.
*/
public static ScrewSize fromString( final String rep )
{
if ( rep == null )
{
return null;
}
for ( ScrewSize val : values() )
{
if ( rep.equals( val.toString() ) )
{
return val;
}
}
return valueOf( rep );
}

/**
* Look up enum constant from {@code Double} ssize.
* @param ssiz Double ssize.
* @return ScrewSize constant matching the ssize.
*/
public static ScrewSize fromSsize( final Double ssiz )
{
if ( ssiz == null )
{
return null;
}
return Holder.ssizes.get( ssiz );
}

private static class Holder
{
private Holder(){}
static final SortedMap <Double, ScrewSize> ssizes;
static
{
SortedMap< Double, ScrewSize> sszs =
new TreeMap <Double, ScrewSize> ();

for ( ScrewSize sz : ScrewSize.values() )
{
sszs.put( sz.getSsize(), sz );
}
ssizes = Collections.unmodifiableSortedMap( sszs );
}
}
}
</untested>

--
Lew

Lew, Aug 17, 2010