Home  >  Article  >  Web Front-end  >  How to convert negative numbers to positive in javascript

How to convert negative numbers to positive in javascript

青灯夜游
青灯夜游Original
2021-04-01 16:34:364409browse

In javascript, you can use the abs() function to convert a negative number into a positive number by taking the absolute value of a negative number; the syntax format is "Math.abs(x)", and the parameter x is the negative value that needs to be converted to a positive number. , for example "Math.abs(-7.25)".

How to convert negative numbers to positive in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

js absolute value (convert negative value to positive)

The absolute value operation of junior high school algebra, syntax:

| -1000 | = 1000;

js code is as follows:

var t = -1000;
Math.abs(t);

Output:

How to convert negative numbers to positive in javascript

[Recommended learning: js basic tutorial]

Introduction to related functions

#abs() method can return the absolute value of a number. The syntax is as follows:

Math.abs(x)

Parameter value:

##x. Returns NaN if x is not a number, or 0 if x is null.
Type Description
Number# The absolute value of
Return value:

Type Description The absolute value of Numberx. Returns NaN if x is not a number, or 0 if x is null.
For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of How to convert negative numbers to positive in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn