JS scientific notation converts numeric characters to break power number positive 21-bit, negative 7-bit automatic conversion limit

Source: Internet
Author: User

The day before yesterday to convert back to the background of the data returned, is the scientific calculation of the number of characters, using online can search for the common two methods of conversion will have a multiple limit, and then searched for a long time, or did not find a good method, although there are some of their own methods of writing, Can still not be like the following two methods can be normal conversion of a variety of positive and negative scientific counts, and break the power of the positive 21-bit, negative 7-bit automatic conversion limit. So compared to the next, spend an afternoon to modify the test, basically general, but only the parameters passed in must be a scientific count of strings.

var num = new number (' 3.54545E-3 ');  3.54545e-7
var sbalance = parsefloat (" -3465.4556645445E-2"); Both of these methods can convert non-standard scientific counts to normal.
Console.log (num);
Console.log (sbalance);

/* Above two methods can be directly converted to scientific count of the string as a number
* However, number type numbers are automatically converted, positive numbers are greater than or equal to 21 bits will automatically convert to scientific count, negative numbers less than or equal to 7 bits will automatically convert to scientific count */

Write a general method, break the power number positive 21-bit, negative 7-bit automatic conversion limit

function Convertnum (NUM_STR) {//parameter must be a string
Scientific notation characters convert to numeric characters, break the number of positive 21-bit and negative 7-bit conversions automatically
Compatible with multiple digits to the left of the decimal point, i.e. ax10^b (AEB), a non-standard range (1≤|A|<10). such as 3453.54E-6 or 3453.54E6
var resvalue = ', power = ', result = null, Dotindex = 0, Resarr = [], sym = '; var numstr = num_str.tostring (); if (numstr[0] = = '-') {//if negative, turn to positive processing, remove the '-' sign first and save '-'. Numstr = NUMSTR.SUBSTR (1); sym = '-'; }console.log (NUMSTR) if ((Numstr.indexof (' E ')! =-1) | | (Numstr.indexof (' e ')! =-1)) {var regExp = new RegExp (' ^ ((\\d+. \\d+) | (\\d+)) [Ee] {1} ((-(\\d+)) | (\\d+))) $ ', ' IG '); result = Regexp.exec (NUMSTR); Console.log (result); if (result! = null) {Resvalue = result[2]; Power = result[5]; result = NULL; } if (!resvalue &&!power) {return false} Dotindex = Resvalue.indexof ('. '); Resvalue = Resvalue.replace ('. ', '); Resarr = Resvalue.split ("); if (number (power) >= 0) {var subres = resvalue.substr (Dotindex); Power = number (power); When the number of powers is greater than the number of digits after the decimal point, add 0 for (var i=0; i<power-subres.length; i++){Resarr.push (' 0 '); } if (Power-subres.length < 0) {Resarr.splice (dotindex+power, 0, '. '); }}else{Power = Power.replace ('-', '); Power = number (power); The index position with a power greater than or equal to the decimal point, preceded by 0 for (var i=0; i<=power-dotindex; i++) {resarr.unshift (' 0 '); } var n = power-dotindex >= 0? 1:-(Power-dotindex); Resarr.splice (n, 0, '. '); }} resvalue = Resarr.join ("); Console.log (Sym+resvalue); return sym+resvalue;
}
Convertnum (' 3.54545E-2 '); 0.0354545

JS scientific notation converts numeric characters to break power number positive 21-bit, negative 7-bit automatic conversion limit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.