Link to home
Start Free TrialLog in
Avatar of newbie27
newbie27Flag for United Kingdom of Great Britain and Northern Ireland

asked on

function to format a fixed digit number

Hello Folks,
I am using attached function to format some fixed digit number but it is giving me an error.
Please can you see if this is not correct?
thanks
s
<script>
function twoDecimal(num,round) {
  if (round) return num.toFixed(2);
  var stringNum = String(num);
  if (stringNum.indexOf('.')==-1) return stringNum+'.00';
  var newNum = stringNum.split('.');
  if (newNum.len==2) {
    newNum[1]=(newNum[1].length>2)?newNum[1].subString(0,2):newNum[1];
  }
  if (newNum[1].length==1) newnum[1]+="0"
  return newNum[0]+'.'+newNum[1]
}
alert(twoDecimal(133.5999,1));
alert(twoDecimal(133.5999));
alert(twoDecimal(133));
alert(twoDecimal(133.5));
 
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Airyck666
Airyck666
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of newbie27

ASKER

thanks