Solved

How to strip off decimal numbers in js?

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 32
Passport Expiry 3 44
insert value of checklistbox checked 4 27
Call a function within the ASP code 4 12
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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

810 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