PDA

View Full Version : parseDecimal


beetle
10-10-2002, 06:05 PM
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// @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 :D<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>

joh6nn
10-10-2002, 11:05 PM
i'd change this:

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

to this:

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

it should do the same thing

beetle
10-11-2002, 01:41 AM
Ah yes, I had forgotten about the negate thingy when I wrote this...

appreciated...

Alex Vincent
10-12-2002, 03:12 AM
:) 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.

beetle
10-12-2002, 05:58 AM
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'. :D

Alex Vincent
10-12-2002, 09:46 PM
Caveat emptor...