denhamd2

10-13-2009, 01:03 PM

Hi,

I'm having a problem with a replace function in my Javascript code. Basically I am taking a number "housevalue" and dividing it and rounding it up. Because the value was sometimes coming in in the format "3,45 houses" I needed to convert it to "3.45 houses" in order to get a proper decimalised number so I could divide it. However, the final outputted number I get always comes out like 0.93 (it always has a "." in it). Is there any way possible of outputting the number in the same format as it came in as - ie having a comma or having a dot (not always having a dot)? Here is my function below, and help would be greatly appreciated!

<script type="text/javascript">

function roundVal(val)

{

var dec = 2;

return Math.ceil(val * Math.pow(10, dec)) / Math.pow(10, dec);

}

function doit(val)

{

var matches = /([^0-9.,]*)([0-9.,]+)([^0-9.,]*)/.exec(val);

matches[2] = roundVal((parseFloat(matches[2].replace(/,/g, '.')) / 12) / 3);

return matches[1] + matches[2] + matches[3];

}

</script>

<script type="text/javascript">

var housevalue = '€19,3534211 EUR';

document.write(doit(housevalue));

</script>

I'm having a problem with a replace function in my Javascript code. Basically I am taking a number "housevalue" and dividing it and rounding it up. Because the value was sometimes coming in in the format "3,45 houses" I needed to convert it to "3.45 houses" in order to get a proper decimalised number so I could divide it. However, the final outputted number I get always comes out like 0.93 (it always has a "." in it). Is there any way possible of outputting the number in the same format as it came in as - ie having a comma or having a dot (not always having a dot)? Here is my function below, and help would be greatly appreciated!

<script type="text/javascript">

function roundVal(val)

{

var dec = 2;

return Math.ceil(val * Math.pow(10, dec)) / Math.pow(10, dec);

}

function doit(val)

{

var matches = /([^0-9.,]*)([0-9.,]+)([^0-9.,]*)/.exec(val);

matches[2] = roundVal((parseFloat(matches[2].replace(/,/g, '.')) / 12) / 3);

return matches[1] + matches[2] + matches[3];

}

</script>

<script type="text/javascript">

var housevalue = '€19,3534211 EUR';

document.write(doit(housevalue));

</script>