# Thread: Removing a single digit from an Double

1. Member
Join Date
Feb 2013
Posts
26
Rep Power
0

## Removing a single digit from an Double

Hey, I need to write a method where i remove the last digit of a double value. So if the original value was "12345" after the method call the value would be "1234" does anyone know part of the API which may do this? I couldn't find anything in the Double API. Can anyone offer any advice on how i might achieve this?

Thanks :)

2. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,725
Rep Power
4

## Re: Removing a single digit from an Double

Well,

1. convert to a long
2. divide by 10
3. reassign to the double.

This does not preserve any digits to the right of the decimal. To do that you need to

1. save fractional part
2. repeat steps 1-3 above

There are probably other ways to do it too.

Regards,
Jim

3. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Removing a single digit from an Double

Depends if "last digit" includes digits to the right of the decimal point.

4. Member
Join Date
Feb 2013
Location
Posts
25
Rep Power
0

## Re: Removing a single digit from an Double

i tried my best to solve this myself and came up with this soltuion. It may have some errors but check it if this can help you.

Convert Double into String
Make substring of this main string till string.length-3
convert back this string into double

Regards
jamil
Last edited by Jamil37037; 03-05-2013 at 10:23 AM. Reason: Firslty provided code so to prevent spoonfeeding

5. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,725
Rep Power
4

## Re: Removing a single digit from an Double

Did you mean string.length() - 1?

Jim

6. Member
Join Date
Feb 2013
Location
Posts
25
Rep Power
0

## Re: Removing a single digit from an Double

No jim i did b=string.length()-3;

and then sent this b as substring parameter substring(0,b);
coz in string double was stored as 12345.0 so i did -3 . Do you think its right approach??

7. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,725
Rep Power
4

## Re: Removing a single digit from an Double

Nothing wrong with your approach and it works fine for whole number doubles. In fact I like it better than my suggestion of type conversion. It's a matter of what is required. Since you were using double I thought that you were considering a general case where, regardless of the last digit (fractions included) that you would remove the last one. So for a number like 123.827289 converted to a String, your method would remove the rightmost digit (9) *if* you used string.length()-1. But then there is the special case(s) like 12345.0 where if you remove the zero and covert back to a double, you haven't changed anything. Your posted method takes care of that.

Depending on what is wanted, multiple cases need to be considered.

Regards,
Jim

8. Member
Join Date
Feb 2013
Location
Posts
25
Rep Power
0

## Re: Removing a single digit from an Double

Originally Posted by jim829
Nothing wrong with your approach and it works fine for whole number doubles. In fact I like it better than my suggestion of type conversion. It's a matter of what is required. Since you were using double I thought that you were considering a general case where, regardless of the last digit (fractions included) that you would remove the last one. So for a number like 123.827289 converted to a String, your method would remove the rightmost digit (9) *if* you used string.length()-1. But then there is the special case(s) like 12345.0 where if you remove the zero and covert back to a double, you haven't changed anything. Your posted method takes care of that.

Depending on what is wanted, multiple cases need to be considered.

Regards,
Jim