# Removing a single digit from an Double

• 02-25-2013, 06:22 PM
HarleyRowland
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 :)
• 02-25-2013, 08:55 PM
jim829
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
• 02-26-2013, 10:36 AM
Tolls
Re: Removing a single digit from an Double
Depends if "last digit" includes digits to the right of the decimal point.
• 03-05-2013, 10:14 AM
Jamil37037
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
• 03-05-2013, 05:09 PM
jim829
Re: Removing a single digit from an Double
Did you mean string.length() - 1?

Jim
• 03-06-2013, 07:56 AM
Jamil37037
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??
• 03-06-2013, 05:59 PM
jim829
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
• 03-07-2013, 07:43 AM
Jamil37037
Re: Removing a single digit from an Double
Quote:

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