I need to get the first 2 non zero digits from a decimal number. How can this be achieved?
Suppose I have number like 0.000235 then I need 0.00023, if the number is 0.000000025666 then my function should return 0.000000025.
Can any one have an idea of how this can be achieved in javascript?
The result should be a float number not a string.
I need to get the first 2 non zero digits from a decimal number. How can this be achieved?
Suppose I have number like 0.000235 then I need 0.00023, if the number is 0.000000025666 then my function should return 0.000000025.
Can any one have an idea of how this can be achieved in javascript?
The result should be a float number not a string.
Share Improve this question edited May 27, 2014 at 11:21 praveen asked May 27, 2014 at 10:46 praveenpraveen 3514 silver badges17 bronze badges 6- What if the number is greater than 1 ? – Denys Séguret Commented May 27, 2014 at 10:51
- I will handle it separately in if condition. – praveen Commented May 27, 2014 at 10:54
- Are you aware that 0.000000025666 is converted to scientific notation 2.5666e-8? This sort of thing interferes with looking for the string '0'. – Paul Commented May 27, 2014 at 11:00
- What answer would you want if the number was 0.0002035 ? – Dan King Commented May 27, 2014 at 11:00
- 2 I think u should specify if the result should be a number type (float) or rather string. It depends if you want just the number to send it somewhere (so doesn't matter how the string representation looks like) or if you want string to display it in nice form. – Piotr Karasiński Commented May 27, 2014 at 11:14
4 Answers
Reset to default 14Here are two faster solutions (see jsperf) :
Solution 1 :
var n = 0.00000020666;
var r = n.toFixed(1-Math.floor(Math.log(n)/Math.log(10)));
Note that this one doesn't round to the smallest value but to the nearest : 0.0256
gives 0.026
, not 0.025
. If you really want to round to the smallest, use this one :
Solution 2 :
var r = n.toFixed(20).match(/^-?\d*\.?0*\d{0,2}/)[0];
It works with negative numbers too.
var myNum = 0.000256
var i = 1
while(myNum < 10){
myNum *= 10
i *= 10
}
var result = parseInt(myNum) / i
With numbers that has that many decimals, you'd have to return a string, as any parsing as number would return scientific notation, as in 0.000000025666
would be 2.5666e-8
function round(n, what) {
var i = 0;
if (n < 1) {
while(n < 1) {
n = n*10;
i++;
}
}
return '0.' + (new Array(i)).join('0') + n.toFixed(what).replace('.','').slice(0,-1);
}
FIDDLE
Please try the below code:
function keepNonZeroDecimalPlaces(number, minDecimalPlaces) {
const numStr = number.toString();
let result = '';
let decimalCount = 0;
for (let i = 0; i < numStr.length; i++) {
const char = numStr.charAt(i);
if (char === '.') {
result += char; // Keep the decimal point
} else if (char !== '0' || decimalCount > 0) {
result += char;
if (char !== '0') {
decimalCount++;
}
if (decimalCount >= minDecimalPlaces) {
break;
}
}
}
return parseFloat(result);
}
// Example usage
const number1 = 0.00003135110744878017;
const result1 = keepNonZeroDecimalPlaces(number1, 3);
console.log(result1); // Output: 0.00000313
const number2 = 0.000010255258583418511;
const result2 = keepNonZeroDecimalPlaces(number2, 4);
console.log(result2); // Output: 0.00000103