Solved

How to strip off decimal numbers in js?

Posted on 2009-04-10
5
446 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…

943 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

5 Experts available now in Live!

Get 1:1 Help Now