Advanced Search

Results 1 to 6 of 6

Thread: String to decimal...

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default String to decimal...

    I should probably know this, but I don't without writing my own function. Here it is: How do I convert a string to a decimal. As in change "7.012" (a string) to 7.012 (a number). "parseFloat" doesn't appear to do it. It rounds it off to 7. And, "parseInt", I would assume is integers only.

    Thanks,
    Stephen

  2. #2
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Looks like your confusing parseInt with parseFloat... ParseInt will round, parseFloat will not round.
    ParseFloat:
    Code:
    <script type="text/javascript">
    window.onload = function(){
      var typeE = "7.012";
      alert("Before parseFloat(): "+typeof(typeE)+": "+typeE);
      typeE = parseFloat(typeE);
      alert("After parseFloat():  "+typeof(typeE)+": "+typeE);
    }
    </script>
    This alerts:
    Code:
    Before parseFloat(): string: 7.012
    
    After parseFloat(): number: 7.012
    ParseInt:

    Code:
    <script type="text/javascript">
    window.onload = function(){
      var typeE = "7.012";
      alert("Before parseInt(): "+typeof(typeE)+": "+typeE);
      typeE = parseInt(typeE);
      alert("After parseInt():  "+typeof(typeE)+": "+typeE);
    }
    </script>
    This alerts:
    Code:
    Before parseInt(): string: 7.012
    
    After parseInt(): number: 7
    Last edited by Nile; 01-04-2009 at 05:22 PM.
    Jeremy | jfein.net

  3. The Following User Says Thank You to Nile For This Useful Post:

    ??? (01-04-2009)

  4. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,704
    Thanks
    43
    Thanked 3,133 Times in 3,097 Posts
    Blog Entries
    12

    Default

    Unless your string also contains trailing non-numeric characters, or you want only its integer component, or change its base, you don't need to parse it at all. It is true though that parseInt will lop off the decimal places (not round the value, actually more like the Math.floor() method). The two parse methods are primarily for removing trailing non-numeric characters and/or converting between bases. In this case all we need do is subtract 0:

    Code:
    var n = "7.012" - 0;
    alert(typeof n + ' ' + n);
    alerts:

    number 7.012
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ??? (01-04-2009)

  6. #4
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Thanks all, I don't know where I went wrong...

  7. #5
    Join Date
    Jan 2008
    Posts
    4,158
    Thanks
    28
    Thanked 623 Times in 619 Posts
    Blog Entries
    1

    Default

    Glad to help you ???. (lol... what a name)
    Jeremy | jfein.net

  8. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Rather than something convoluted like subtracting zero, to coerce to number all one need do is apply unary plus:
    Code:
    var n = +"7.012";
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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