Last active
August 22, 2024 05:03
-
-
Save jiggzson/b5f489af9ad931e3d186 to your computer and use it in GitHub Desktop.
Converts a javascript number from scientific notation to a decimal string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Removes the minus sign from the beginning of the string | |
* | |
* @param str | |
* @returns an array with the first item as true if a minus | |
* was found and the string minus the minus sign. | |
*/ | |
function stripSign(str) { | |
// Check if it has a minus sign | |
let hasMinus = str.charAt(0) === '-'; | |
// Remove it if it does | |
if (hasMinus || str.charAt(0) === '+') { | |
str = str.substring(1); | |
} | |
return [hasMinus, str]; | |
} | |
/** | |
* Converts a string from scientific notation form to decimal form | |
* | |
* @param str | |
* @returns | |
*/ | |
function scientificToDecimal(str) { | |
if (/\d+\.?\d*e[\+\-]*\d+/i.test(str)) { | |
let isNegative, isSmall; | |
// Remove the sign by slicing the string | |
[isNegative, str] = stripSign(str); | |
// Split it into coefficient and exponent | |
let [c, e] = str.toLowerCase().split('e'); | |
// Split the coefficient into the whole and decimal portion | |
let [w, d] = c.split('.'); | |
// Provide and empty sting for safety if in the form n(e)n | |
d = d || ''; | |
// The total length of the string | |
let length = w.length + d.length; | |
// The total string minus the dot | |
let numString = w + d; | |
// If it's small then we need to calculate the leading zeros | |
// The shift of the decimal place to the left | |
const dotLocation = w.length + Number(e); | |
// Is the dot needed or not | |
const dot = dotLocation === length ? '' : '.'; | |
let value; | |
if (dotLocation <= 0) { | |
// Join the value but pad after the dot with zeroes | |
value = `0${dot}${'0'.repeat(Math.abs(dotLocation))}${numString}`; | |
} | |
else if (dotLocation > length) { | |
value = `${numString}${'0'.repeat(Math.abs(dotLocation - length))}`; | |
} | |
else { | |
value = `${numString.substring(0, dotLocation)}${dot}${numString.substring(dotLocation)}`; | |
} | |
return isNegative ? '-' + value : value; | |
} | |
return str; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var expect = function(value) { | |
return { | |
toEqual: function(otherValue) { | |
if(value !== otherValue) { | |
console.error(value+' did not yield the correct result!'); | |
} | |
} | |
}; | |
}; | |
expect(scientificToDecimal(2.5e25)).toEqual('25000000000000000000000000'); | |
expect(scientificToDecimal(-1.123e-10)).toEqual('-0.0000000001123'); | |
expect(scientificToDecimal(-1e-3)).toEqual('-0.001'); | |
expect(scientificToDecimal(-1.2e-2)).toEqual('-0.012'); | |
expect(scientificToDecimal(12.12)).toEqual(12.12); | |
expect(scientificToDecimal(141120000000000000)).toEqual(141120000000000000); | |
expect(scientificToDecimal('0')).toEqual(0); | |
expect(scientificToDecimal(1.23423534e-12)).toEqual(0.00000000000123423534); |
Yes, it doesn't work for large values. This number:
const foo = scientificToDecimal("3.4028236692093846346e+38");Is incorrectly parsed as
340282366920938500000000000000000000000
.
it doesnt work for me if number is "115792089237316195423570985008687907853269984665640564039457584007913129639935"
it returns "115792089237316200000000000000000000000000000000000000000000000000000000000000"
did you find any solution for this?
did you find any solution for this?
Yes, check out from-exponential. Also in case you're doing Ethereum development, check out my package evm-bn, which you can use like this:
import { toBn } from "evm-bn";
// 3141500000000000000
const foo: BigNumber = toBn("3.1415");
I write the simple to convert the SN to RN checkout this code
https://github.com/zohaibtahir/Math-Calculations/blob/main/Scientific-notation-to-real_number.js
Updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the heads-ups, @elhoucine!