Velocity Reviews > Java > How would you get rid of lagging zeros?

# How would you get rid of lagging zeros?

Ramon F Herrera
Guest
Posts: n/a

 08-16-2010

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

Daniel Pitts
Guest
Posts: n/a

 08-16-2010
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/>

Arved Sandstrom
Guest
Posts: n/a

 08-16-2010
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

Robert Tomsick
Guest
Posts: n/a

 08-16-2010
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 ---

Lew
Guest
Posts: n/a

 08-17-2010
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

Ramon F Herrera
Guest
Posts: n/a

 08-17-2010
On Aug 16, 7:02*pm, Lew <(E-Mail Removed)> 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
Guest
Posts: n/a

 08-17-2010
On Aug 16, 6:23*pm, "Arved Sandstrom" <(E-Mail Removed)> 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

Arved Sandstrom
Guest
Posts: n/a

 08-17-2010
Ramon F Herrera wrote:
> On Aug 16, 6:23 pm, "Arved Sandstrom" <(E-Mail Removed)> 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

Stefan Ram
Guest
Posts: n/a

 08-17-2010
Ramon F Herrera <(E-Mail Removed)> 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 );

Lew
Guest
Posts: n/a

 08-17-2010
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