math - How to check for exponential values in Javascript - Stack Overflow

I am streaming data from a CSV file and I am pushing certain values to an array. Some of these values a

I am streaming data from a CSV file and I am pushing certain values to an array. Some of these values are very large, and may be exported as exponential values (e.g. 5.02041E+12) and some may be normal values. I'd like to have an if statement that checks to see if these values are exponential or not, and if they are I will pass them to a function that converts them into 'normal' numbers (e.g. 5020410000000). Is there a quick way to do this?

(These values are passed to an API call which is why they need to be converted to 'normal' values)

Example of what this may look like:

valueOne = 5.02041E+12;
valueTwo = 1234;

if (valueOne.isExponential) {
  **pass to converting function**
}

//Output = 5020410000000

if (valueTwo.isExponential) {
  **pass to converting function**
}

//Output = 1234 (unchanged)

I'd expect all values in the array to therefore be 'normal' values (i.e. NOT is exponential form)

I am streaming data from a CSV file and I am pushing certain values to an array. Some of these values are very large, and may be exported as exponential values (e.g. 5.02041E+12) and some may be normal values. I'd like to have an if statement that checks to see if these values are exponential or not, and if they are I will pass them to a function that converts them into 'normal' numbers (e.g. 5020410000000). Is there a quick way to do this?

(These values are passed to an API call which is why they need to be converted to 'normal' values)

Example of what this may look like:

valueOne = 5.02041E+12;
valueTwo = 1234;

if (valueOne.isExponential) {
  **pass to converting function**
}

//Output = 5020410000000

if (valueTwo.isExponential) {
  **pass to converting function**
}

//Output = 1234 (unchanged)

I'd expect all values in the array to therefore be 'normal' values (i.e. NOT is exponential form)

Share Improve this question edited Apr 17, 2019 at 10:02 Caleb Whittington asked Apr 17, 2019 at 9:46 Caleb WhittingtonCaleb Whittington 1012 silver badges10 bronze badges 5
  • Could you provide a sample input please? – Jonas Wilms Commented Apr 17, 2019 at 9:48
  • @JonasWilms Edited just now- hopefully this adds some clarirty – Caleb Whittington Commented Apr 17, 2019 at 10:03
  • It would simpler don't check but just explicitly represent all values in "normal" form – MBo Commented Apr 17, 2019 at 10:04
  • Do console.log(valueTwo) and be surprised. – Jonas Wilms Commented Apr 17, 2019 at 10:06
  • @MBo trouble is if non-exponential values are passed to my function that converts them to 'normal' values, the function breaks. I thought it would be easier to add a simple check for exponential values rather than refactor my converting function – Caleb Whittington Commented Apr 17, 2019 at 10:14
Add a ment  | 

3 Answers 3

Reset to default 1

Numbers are numbers, the values 5.02041E+12 and 5020410000000 do not differ internally:

// values in code:
var withe=5.02041E+12;
var withoute=5020410000000;
console.log(withe,"?=",withoute,withe===withoute);

// values parsed from strings:
var stringwithe="5.02041E+12";
var stringwithoute="5020410000000";
var a=parseFloat(stringwithe);
var b=parseFloat(stringwithoute);
console.log(a,"?=",b,a===b);

And you can also see that when you simply display a number, it will not use the scientific notation by default, actually you would have to ask for it via using toExponential()


One thing you can worry about is the internal precision of Number. It has a method isSafeInteger() and various fields, like MAX_SAFE_INTEGER. Surpassing that value can lead to unexpected results:

var a=Number.MAX_SAFE_INTEGER;
console.log("This is safe:",a,Number.isSafeInteger(a));
a++;
for(var i=0;i<5;i++)
  console.log("These are not:",a++,Number.isSafeInteger(a));

So the loop can not increment a any further, because there is no such number as 9007199254740993 here. The next number which exists after 9007199254740992 is 9007199254740994. But these numbers are more than 1000x greater than the 5020410000000 in the question.

You can just use toPrecision on every number and ensure it converts

const valueOne = 5.02041E+12;
const valueTwo = 1234;

const precisionValueOne = valueOne.toPrecision(); // "5020410000000"
const precisionValue2 = valueTwo.toPrecision(); // "1234"

You can then, optionally convert it back to numbers:

sanitizedValueOne = Number(precisionValueOne); // 5020410000000
sanitizedValueTwo = Number(precisionValueTwo); // 1234

Do a RegExp match for E+ and probably for E-

The 'number' you are starting with must be text, but you should do a bit of sanity checking too. Might be a good idea to check whether it is larger than MaxInt before you try any Math-based conversions.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745355100a4624067.html

相关推荐

  • math - How to check for exponential values in Javascript - Stack Overflow

    I am streaming data from a CSV file and I am pushing certain values to an array. Some of these values a

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信