Solved

function to format a fixed digit number

Posted on 2008-06-13
3
233 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:newbie27
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Airyck666 earned 500 total points
ID: 21780787
Since the only time you can't use the .toFixed to solve this is if !round and there's more than 2 decimal places.  So try somethig this code.
<script>

function twoDecimal(num,round) {

    

    var stringNum = String(num);

    var newNum = stringNum.split('.');

    if  (newNum.length > 1  && newNum[1].length > 1 && !round) {

        newNum[1]=(newNum[1].length > 2) ? newNum[1].substring(0,2) : newNum[1];

        return newNum[0]+'.'+newNum[1];

    } else {

        return String(num.toFixed(2));

    }

}

alert(twoDecimal(133.5999,1));

alert(twoDecimal(133.5999));

alert(twoDecimal(133));

alert(twoDecimal(133.5));

 

</script>

Open in new window

0
 
LVL 2

Assisted Solution

by:Airyck666
Airyck666 earned 500 total points
ID: 21780800
oops... you don't need the String object creation at line 10.
return num.toFixed(2);

Open in new window

0
 
LVL 8

Author Closing Comment

by:newbie27
ID: 31466970
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now