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>
// @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>