1

I have a requirement to format a no to get 3 significant digit after a decimal in javascript..

detail about the significant digit can be found here http://www.usca.edu/chemistry/genchem/sigfig.htm

here are the rule for significant digit

1) ALL non-zero numbers (1,2,3,4,5,6,7,8,9) are ALWAYS significant.

2) ALL zeroes between non-zero numbers are ALWAYS significant.

3) ALL zeroes which are SIMULTANEOUSLY to the right of the decimal point AND at the end of the number are ALWAYS significant.

4) ALL zeroes which are to the left of a written decimal point and are in a number >= 10 are ALWAYS significant.

i want function like function significantDigit(no, noOfDecimal) { return signifcantNo }

Example of significant digits. 48,923 has five significant digit..significantDigit(no,3) should return 48923 3.967 has four significant digit..significantDigit(no,3) should return 3.967 0.00104009 has six significant digit,..significantDigit(no,3) should return .00104

shyam_
  • 2,370
  • 1
  • 27
  • 50

3 Answers3

0

hope this helps

var anumber=123.45
anumber.toPrecision(6) //returns 123.450 (padding)
anumber.toPrecision(4) //returns 123.5 (round up)
anumber.toPrecision(2) //returns 1.2e+2 (you figure it out!)

thanks for the edited question this one ll solve your requirement

var anumber = 123.4050877
var str = anumber.toPrecision(6)
var a = [];
a= JSON.parse("[" + str + "]");
alert(a.length)
for(var i=6;i<=a.length;i--){
    if(a[i]=="0"){
         a.splice(i, 1);
    }

}
alert(a)
kavinhuh
  • 739
  • 1
  • 9
  • 30
  • sorry this is not the significant digit. for eg. 123.45 has two significant digit and it should display just 123.45.. not the extra zero.. – shyam_ May 11 '15 at 08:08
  • please check for this no..8.1000 it has 5 significant digit..and if i say give me 3 significant digit after decimal, it should return 8.100 – shyam_ May 11 '15 at 08:55
  • 0.00104009 has six significant digit,..significantDigit(no,3) should return .00104 , please check for this no..8.1000 it has 5 significant digit..and if i say give me 3 significant digit after decimal, it should return 8.100 these two examples contradicts – kavinhuh May 11 '15 at 09:49
0

this may not be the complete solution..but its correct to the most extent (i think) it works really great for very big decimal no..this will give you most significant digit after decimal

        function signiDigit(val, noOfdecimalPoint) {
        debugger;
    var noString = String(val);
    var splitNo = noString.split(".");    
    if (splitNo.length > 1) {
        if(parseInt(splitNo[0])!==0 ||splitNo[0]==='' )
        {
            if(noString.length - 1 > noOfdecimalPoint)
            {
                return Math.round(val);
            }else
            {
                return val;
            }            
        }else
        {
            var noafterDecimal =String(parseInt(splitNo[1]));
            if(noafterDecimal.length > noOfdecimalPoint)
            {
                return parseFloat(val.toFixed(splitNo[1].indexOf(noafterDecimal) + noafterDecimal.length-1));
            }
            else{
                return val;
            }            
        }
}}
var no = signiDigit(9.999,3);
        alert(no);

here is the fiddeler link http://jsfiddle.net/n1gt4k90/4/

this is not the complete significant no but mix of significant and rounding.

shyam_
  • 2,370
  • 1
  • 27
  • 50
0

i have found a java code here thanks to Pyrolistical

Rounding to an arbitrary number of significant digits

public static double roundToSignificantFigures(double num, int n) {
if(num == 0) {
    return 0;
}

final double d = Math.ceil(Math.log10(num < 0 ? -num: num));
final int power = n - (int) d;

final double magnitude = Math.pow(10, power);
final long shifted = Math.round(num*magnitude);
return shifted/magnitude;}

i have converted this to a javascript code, this can be found at http://jsfiddle.net/f6hdvLjb/4/ javascript code is

function roundToSignificantFigures(num, n) {

if(num === 0) {
    return 0;
}

var d = Math.ceil(Math.log10(num < 0 ? -num: num));
var power = n - parseInt(d);

var magnitude = Math.pow(10, power);
var shifted = Math.round(num*magnitude);
alert(shifted/magnitude);

return shifted/magnitude;

} roundToSignificantFigures(6666666.0412222919999,3);

i think this is what the significant digit logic.

Community
  • 1
  • 1
shyam_
  • 2,370
  • 1
  • 27
  • 50