Solved

How to strip off decimal numbers in js?

Posted on 2009-04-10
5
444 Views
Last Modified: 2012-05-06
Hi

I have a aspx and js. I need to let the calculated result of the textbox to strip off the number to have 6 decimal places.

How can I strip off objRec.Fields("countervalue").value ?
e.g. 1/0.9 * 1 = 1.111111111111111

How can it be 1.111111 ?


function Calc(obj) {

          var objRec = xmlindata.recordset;

          if ( objRec ) {

            objRec.absolutePosition = obj.recordNumber;

            var amountVal = eval( objRec.Fields("amount").value )

            var rateVal   = eval( objRec.Fields("rate").value )

            objRec.Fields("countervalue").value = amountVal * rateVal

          }

        }

Open in new window

0
Comment
Question by:techques
5 Comments
 
LVL 3

Accepted Solution

by:
QualitySoftwareDevelopment earned 50 total points
ID: 24117846
I think this will do it
var num = 10;
var result = num.toFixed(6);  (should return 10.000000)
0
 
LVL 5

Assisted Solution

by:xtravagan
xtravagan earned 50 total points
ID: 24117908
Or if you want to work with a string.

    var dot = n.indexOf('.');
    var str = n;
    if ( dot > 0 )
      str = n.substr(0, dot + 7);

This will truncate, toFixed will round. To use toFixed on a string do this

    var ii = new Number(n);
    var str = "" + ii.toFixed(6);
0
 
LVL 41

Assisted Solution

by:HonorGod
HonorGod earned 50 total points
ID: 24118040
If you would prefer to not lose information, you may want to make use of

Math.round( )..

For example, with something like:

So, if we did:

alert( format( 3.14159, 4 ) )

the output would be 3.1416
function format( val, places ) {

  var mul = Math.pow( 10, places )

  return ( Math.round( val * mul ) / mul ).toFixed( places )

}

Open in new window

0
 

Author Comment

by:techques
ID: 24118081
all are good solution
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24118121
If all are good, then why a "B"?

Thanks for the assist and points.

Good luck & have a Happy Easter.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now