Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By Jamil37037

Thread: Removing a single digit from an Double

  1. #1
    HarleyRowland is offline Member
    Join Date
    Feb 2013
    Posts
    26
    Rep Power
    0

    Default 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. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default 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
    3. add back fractional part


    There are probably other ways to do it too.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: Removing a single digit from an Double

    Depends if "last digit" includes digits to the right of the decimal point.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default 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 09:23 AM. Reason: Firslty provided code so to prevent spoonfeeding
    DarrylBurke and jim829 like this.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Removing a single digit from an Double

    Did you mean string.length() - 1?

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default 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. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: Removing a single digit from an Double

    Quote Originally Posted by jim829 View Post
    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
    Thanks mate i always follow your suggestions...

Similar Threads

  1. Removing decimals from double
    By RojBlake in forum New To Java
    Replies: 2
    Last Post: 08-18-2012, 03:06 PM
  2. Genrate Single LL from Double LL
    By SHE in forum New To Java
    Replies: 4
    Last Post: 03-26-2011, 08:18 AM
  3. Removing the double values?
    By Lund01 in forum Advanced Java
    Replies: 13
    Last Post: 11-17-2010, 11:34 AM
  4. Removing duplicates from double Arrays
    By jhong253 in forum New To Java
    Replies: 3
    Last Post: 10-30-2010, 10:32 PM
  5. Java/SQL Removing double data
    By Subhero in forum AWT / Swing
    Replies: 2
    Last Post: 05-13-2010, 03:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •