Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to strip off decimal numbers in js?

Posted on 2009-04-10
5
Medium Priority
?
455 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
[X]
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
5 Comments
 
LVL 3

Accepted Solution

by:
QualitySoftwareDevelopment earned 150 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 150 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 150 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

722 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