Click to See Complete Forum and Search --> : splitting up a number.
Bryan Tuffin
09-19-2004, 12:38 AM
Can anyone help me with this? I have a number in a variable that has four digits for example 4488. Is there any way to take just the first two digits using JavaScript and put them in another variable so that the second variable has 44? Any help here would be greatly appreciated.
Charles
09-19-2004, 06:49 AM
<script type="text/javascript">
<!--
n = 4488;
m = n.toString ().match (/\d{2}/);
alert (m);
// -->
</script>
Warren86
09-19-2004, 07:47 AM
Bryan:
Try this:
<HTML>
<Head>
<Script Language=JavaScript>
var newValue = 0;
function splitValue(isValue){
splitStr = isValue.toString();
splitStr = splitStr.slice(0,2);
alert("These are the first two digits, "+splitStr+" \nas a character string.");
// this converts the splitStr back into a numeric value
newValue = parseInt(splitStr);
alert(newValue);
}
</Script>
</Head>
<Body>
<input type=button Value="Split This" onClick="splitValue(4488)">
</Body>
</HTML>