Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to strip off decimal numbers in js?

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

839 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