0

I'm trying to format a double, such as 99928.000000 to read only 99928. I know that I could change it to an integer, but the data I'm reading in has leading 0's that integers don't pick up. Could I somehow format it to read only 99928.

String result = String.format("%f %s,%s (%1.3f; %1.3f)",zip, city, state, latitude,longitude); 

Current code^^ Thanks for your time.

3
  • 2
    Er... what data types are the variables currently if they keep leading zeroes? Commented Apr 1, 2014 at 22:06
  • Post your initial string. Commented Apr 1, 2014 at 22:06
  • 1
    It makes more sense to store a zip code as a String than as a double. You can't do arithmetic with them (I live halfway between Los Angeles and San Francisco perhaps?) Commented Apr 1, 2014 at 22:13

5 Answers 5

0

You've specified three decimal places by writing .3, so you're getting them. If you don't want them, don't specify them.

I don't know what you mean by 'leading zeros that integers don't pick up'. Neither doubles nor integers contain leading zeros.

Sign up to request clarification or add additional context in comments.

Comments

0

Use DecimalFormat to create a String with leading zeroes to format a zip code. With this code:

DecimalFormat pattern = new DecimalFormat("00000"); String zip = pattern.format(99928.0); System.out.println(String.format("%s", zip)); 

The output is:

99928 

Incidentally, because there are leading zeroes in a zip code, it shouldn't be read as any numeric data type. You should read and store it as a String.

Comments

0

I am guessing you are storing 99928.000000 as a string, if you are able to keep leading zeroes.

In this case, you should be able to get what you want by using string splitting on the decimal point.

String foo = "99928.000000" foo = foo.split("\\.")[0] 

Comments

0

Could you please try using DecimalFormat , it says

# Number Yes Digit, zero shows as absent 

For more details please refer here

Comments

0

for zip code you shall mask it with "%5.0f", as following

String result = String.format("%5.0f %s,%s (%1.3f; %1.3f)", zip, city, state, latitude, longitude); 

may be this could help

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.