How would you get rid of lagging zeros?

R

Ramon F Herrera

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
 
D

Daniel Pitts

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.
 
A

Arved Sandstrom

Ramon said:
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
 
R

Robert Tomsick

Daniel said:
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 ---
 
L

Lew

Ramon said:
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.

Do not multi-post. Bad, bad!

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

Ramon F Herrera

> Do not multi-post.  Bad, bad!

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
 
R

Ramon F Herrera

> 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
 
A

Arved Sandstrom

Ramon said:
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
a bit anal about numbers.

AHS
 
S

Stefan Ram

Ramon F Herrera said:
"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 );
 
L

Lew

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.
 
L

Lew

Ramon said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top