Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: parseDecimal

  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts

    parseDecimal

    here's a function to accompany the existing parseInt and parseFloat functions. However, unlike these two, my parseDecimal function will strip all characters that are non-numeric before attemting to provide a return value...plus you can provide any number of decimal places to round it to or truncate it at...here's the function
    Code:
    // @d 		String. Required.
    // @zeros 	Mixed. Optional.
    // @trunc 	Boolean. Optional.
    
    function parseDecimal(d, zeros, trunc) {
    	d=d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g,"");
    	while (d.indexOf(".") != d.lastIndexOf("."))
    		d=d.replace(/\./,"");
    	if (typeof zeros == 'undefined' || zeros == "") {
    		return parseFloat(d);
    		}
    	else {
    		var mult = Math.pow(10,zeros);
    		if (typeof trunc == 'undefined' || (trunc) == false)
    			return parseFloat(Math.round(d*mult)/mult);
    		else
    			return parseFloat(Math.floor(d*mult)/mult);
    		}
    	}
    And here's a nifty sample page I made to show it off
    Code:
    <html>
    <head>
    <title>parseDecimal</title>
    <script>
    function parseDecimal(d, zeros, trunc) {
    	d=d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g,"");
    	while (d.indexOf(".") != d.lastIndexOf("."))
    		d=d.replace(/\./,"");
    	if (typeof zeros == 'undefined' || zeros == "") {
    		return parseFloat(d);
    		}
    	else {
    		var mult = Math.pow(10,zeros);
    		if (typeof trunc == 'undefined' || (trunc) == false)
    			return parseFloat(Math.round(d*mult)/mult);
    		else
    			return parseFloat(Math.floor(d*mult)/mult);
    		}
    	}
    </script>
    </head>
    
    <body>
    <form>
    Take <input type="text" name="source"> and
    <select name="format">
    <option>round to</option>
    <option>truncate at</option>
    </select>
    <select name="places">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
    </select>
     decimal places<br>
    <input type="button" value="parseDecimal" onClick="this.form.output.value = parseDecimal(this.form.source.value, this.form.places.selectedIndex+1, this.form.format.selectedIndex)"> including parameters<br>
    <input type="button" value="parseDecimal" onClick="this.form.output.value = parseDecimal(this.form.source.value)"> excluding parameters<br>
    <input type="button" value="parseFloat" onClick="this.form.output.value = parseFloat(this.form.source.value)"> for comparison<br>
    <input type="button" value="parseInt" onClick="this.form.output.value = parseInt(this.form.source.value)"> for comparison<br>
    <input type="text" readonly="true" name="output">
    </form>
    </table>
    </body>
    </html>
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    i'd change this:

    d=d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g,"");

    to this:

    d=d.replace(/[^\d\.]/g,"");

    it should do the same thing
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah yes, I had forgotten about the negate thingy when I wrote this...

    appreciated...
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts
    What happens when I feed it a number in scientific notation?

    1e+03

    Half of scientific notation is a decimal... but the other half isn't.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Alex Vincent
    What happens when I feed it a number in scientific notation?

    1e+03

    Half of scientific notation is a decimal... but the other half isn't.
    Hehe, I think that's 'pushing it'.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #6
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,459
    Thanks
    1
    Thanked 23 Times in 21 Posts
    Caveat emptor...
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog


  •  

    Posting Permissions

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