In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to convert numbers into strings in javascript". In daily operation, I believe many people have doubts about how to convert numbers into strings in javascript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to convert numbers into strings in javascript". Next, please follow the editor to study!
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Convert numbers to strings in javascript
Method 1: use the plus sign "+" operator
When a value is added to an empty string, JavaScript automatically converts the value to a string.
Var n = 123 typeof n = n + "; console.log (typeof n); / / return type is string
Method 2: use the toString () method
ToString () is the prototype method of the Object type, and after inheriting this method, the Number subclass overrides toString () to allow you to pass an integer parameter and set the display mode. The number defaults to the decimal display mode, which can be changed by setting parameters.
1) if the argument is omitted, the toString () method takes the default mode and converts the number directly to a numeric string.
Var a = 1.000 console.log var b = 0.0001 console.log var c = 1e Mui 1x console.log (b.toString ()); / / returns the string "1" (c.toString ()); / / returns the string "0.0001" (c.toString ()); / / returns the string "0.0001"
The toString () method outputs integers and floating-point numbers directly, retaining decimal places. The zero at the end of the decimal place is cleared. But for scientific counting, it will be converted to a floating-point number if conditions permit, otherwise the string will be output in the form of scientific counting.
Var a = 1eMui 14bot console.log (a.toString ()); / / returns the string "1e-14"
By default, the toString () method returns a decimal numeric string regardless of the mode in which the numeric value is represented. Therefore, for octal, binary, or hexadecimal numbers, the toString () method converts them to decimal values before outputting them.
Var a = 010; / / octal value 10var b = 0x10; / / hexadecimal value 10console.log (a.toString ()); / / return string "8" console.log (b.toString ()); / / return string "16"
2) if the parameter is set, the toString () method converts the value to the corresponding binary value based on the parameter, and then outputs it as a string representation.
Var a = 10; / / decimal value 10console.log (a.toString (2)); / / return binary number string "1010" console.log (a.toString (8)); / / return octal number string "12" console.log (a.toString (16)); / / return binary number string "a"
Method 3: convert a number to a decimal format string using the toFixed (), toExponential (), or toPrecision () method
When you use the toString () method to convert a numeric value to a string, you cannot retain decimal places. This is undoubtedly inconvenient for professional fields such as currency formatting and scientific counting to output and display numbers. To this end, JavaScript provides three specialized methods, as described below.
1) toFixed ()
ToFixed () converts a numeric value to a string and displays the specified number of digits after the decimal point.
Var a = 10 position console.log (a.toFixed (2)); / / return the string "10.00" console.log (a.toFixed (4)); / / return the string "10.0000"
2) toExponential ()
The toExponential () method is specifically used to convert numbers into strings in the form of scientific counting.
Var a = 123456789 console.log (a.toExponential (2)); / / return the string "1.23e+8" console.log (a.toExponential (4)); / / return the string "1.2346e+8"
The argument to the toExponential () method specifies the number of decimal places to keep. The omitted part is dealt with by rounding.
3) toPrecision ()
The toPrecision () method is similar to the toExponential () method, except that it specifies the number of significant digits instead of the number of decimal places.
Var a = 123456789 console.log (a.toPrecision (2)); / / return the string "1.2e+8" console.log (a.toPrecision (4)); / / return the string "1.235e+8". This is the end of the study on "how to convert numbers into strings in javascript". I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.