


Proficient in numerical calculations in Javascript series_javascript skills
More intuitively:
---------------------
Example: var a=32.6;
var b=67;
var c=9e5;
---------------------
For numeric types, if you want to convert to scientific notation, you can use the toExponential() method, This method accepts a parameter, indicating that the decimal multiple is to be output:
---------------------
Example:
var a=78.9;
alert(a.toExponential(1));
---------------------
Formatting problem of values in js
Formatting output of numbers is a very meaningful thing. For example, many times, we hope that a number can be output as a string in a specified format. Take 26.9878 as an example, we may hope that it can retain two digits. Express it as a decimal, that is, the result is 26.99, or for 0.345678, we hope to be able to output it according to the percent sign and retain two decimal places, that is, the result is 34.57%. For example, we want to display the number 56456456 according to scientific notation, and Retaining two decimal places, the result is 5.65e 7. There are of course many similar examples.
So in the Javascript standard, does it provide support for these formatted outputs? It can be said that Javascript also provides partial support, but it is not complete. For details, you can look at the use of Number objects. , which provides some formatted output for numbers. Several functions are as follows:
toExponential([fractionDigits]): Return the number in scientific notation format, where fractionDigits value is the number of digits retained after the decimal point.
toFixed([fractionDigits]): Returns the number with the specified number of decimal places, where fractionDigits is the number of digits left after the decimal point.
toPrecision([precision]): Return the number to the specified precision (this precision does not refer to the number of decimal places), where precision is the specified precision value.
If you don’t know how to use the above function, let me give you a simple example:
var num=56.45678;
var rs1=num.toExponential(2);//The value of rs1 is 5.65e 1
var rs2=num.toFixed (2);//The value of rs is 56.45
var rs3=num.toPrecision(2);//The value of rs is 56
Although these methods provided by the Number object can solve the problem There are many digital conversion problems, but it is still not satisfactory for many situations, such as support for percent signs, etc.
In order to solve this problem and provide more powerful and flexible digital formatting requirements, JsJava specifically provides Javascript custom classes for support. You can download jsjava-1.0.js and quote it
src/jsjava/text/NumberFormat.js or directly reference jslib/jsjava-1.0.js, for example:
<script> <br>var nf=new DecimalFormat(); <br>nf.applyPattern("000.000%"); <br>var res=nf.format(-0.893566); <br>document.write(res "<br>" ); <br>nf.applyPattern("0000.00"); <br>var res=nf.format(-53.385967); <br>document.write(res "<br>"); <br>nf.applyPattern ("0000.000E00"); <br>var res=nf.format(53.385967); <br>document.write(res "<br>"); <br></script>
The displayed result is:
-89.357%
-53.39
5338.597e-2

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In Python programming, errors are a common occurrence. Among them, ValueError is a common type of error, usually caused by the parameters passed to the function not conforming to the type or range expected by the function. Therefore, Python raises ValueError when a value cannot be converted or is not as expected. This article will introduce how to correctly solve the problem of ValueError. We'll explore how to recognize and resolve ValueError in Python and how to prevent it from happening. 1. Knowledge

Use Python's abs() function to get the absolute value of a value. Code example The absolute value is the non-negative value of a value, that is, the distance from the origin. In Python, we can use the abs() function to get the absolute value of a numerical value. The abs() function accepts a number as a parameter and returns the absolute value of the number. Regardless of whether the input is a positive number, a negative number, or zero, the abs() function returns a nonnegative number. Here are some code examples that use the abs() function to get the absolute value: #Example 1: Get the absolute value of an integer

Use Java's Math.max() function to compare the size of two values and return the larger value. In Java, we often need to compare the size of two values and return the larger value. Fortunately, java has a very convenient built-in function Math.max() to help us complete this task. The Math.max() function takes two parameters, compares their sizes and returns the larger value. Below is a simple code example showing how to use Math.max(

In PHP development, data type conversion is one of the problems that developers often face. Especially when processing user input or getting data from a database, you often encounter situations where you need to cast the data to a numeric type. This article will discuss common problems and solutions for coercing data into numeric values in PHP, and provide specific code examples. Problem description: In PHP, there are often situations where strings or other types of data need to be converted into numerical types. For example, the form data entered by the user is passed to the backend in the form of a string

Recently, "Zhu Xian 2", the new work of "Zhu Xian", the strongest IP in the hands of Perfect World, has launched its first "Inaction Test". This test will delete files in a limited number and support both ends. For the first test of "Zhu Xian 2", in addition to various bug issues, ugly eating is also a problem that players participating in the test complained about most. Netizens said that this was the first time they saw fashion with added attributes. Regarding this point, the official "Zhu Xian 2" quickly issued an announcement last night, stating that the attribute values of all mall appearances will be deleted. It seems that the officials are still listening to players’ opinions. The following is the original text of the official announcement: Hello, fellow immortals: Xiao Mu Ling has carefully read and recorded the issues reported by all the immortal friends since the test. Thank you very much for your encouragement and opinions. After receiving these opinions, Xiao Muling brought some comments from the woman

Detailed explanation of the method of converting numbers to letters in PHP In PHP programming, sometimes we need to convert numbers into corresponding letters, such as converting 1 to A, 2 to B, and so on. This conversion is often used in some application scenarios, such as generating random verification codes, processing user-entered numbers, etc. This article will introduce in detail the method of converting numerical values to letters in PHP, including specific code examples. Method 1: Use ASCII code for conversion. In the ASCII code table, the uppercase letters A-Z correspond to ASCII codes 65-90 respectively.

A complete guide to converting PHP numerical values to date format. In web development, we often encounter situations where we need to convert numerical values to date format. As a popular server-side programming language, PHP provides a wealth of date processing functions and methods that can help us easily convert numerical values into date format. This guide will give you a detailed introduction to how to convert numeric values to date format in PHP and provide specific code examples. 1. Use the date() function The date() function in PHP is one of the most commonly used date processing functions.

How to use the MAX function in MySQL to find the maximum value in the data table Introduction: In MySQL, we often need to perform various queries and analysis on the data table, including finding the maximum value in the data table. The maximum value in a data table can be easily found using the MAX function and is very useful when further processing the data. This article will introduce how to use the MAX function to find the largest value in the data table, and give corresponding code examples. 1. Introduction to the MAX function The MAX function is an aggregate function in MySQL. Use
