Home Web Front-end JS Tutorial Javascript decimal rounding implemented in multiple ways_Basic knowledge

Javascript decimal rounding implemented in multiple ways_Basic knowledge

May 16, 2016 pm 05:45 PM
rounding decimal

Use Javascript to get the two decimal places after the float type. For example, 22.127456 is changed to 22.13. How to do it?
1. The stupidest way

Copy the code The code is as follows:

function get()
{
var s = 22.127456 "";
var str = s.substring(0,s.indexOf(".") 3);
alert(str ; >
The code is as follows:

3. He is smarter
.....




Copy code


The code is as follows:

<script> <strong>var num=22.127456; </strong> alert( Math.round(num*100)/100); <br></script>

Copy code


The code is as follows:

<script> <strong>var num=22.127456; </strong>alert( num.toFixed(2)); <br></script>
Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to convert integer to decimal in javascript How to convert integer to decimal in javascript Nov 03, 2021 pm 05:59 PM

In JavaScript, you can use the toFixed() function to convert an integer into a decimal. This function can convert an integer into a number with a specified number of decimal places; the syntax is "number.toFixed(x)", and the parameter "x" specifies the number of decimal places. .

How to round a number using Math.round() method in Java? How to round a number using Math.round() method in Java? Nov 18, 2023 pm 01:11 PM

How to round a number using Math.round() method in Java? Overview: The Math class in Java provides a series of convenient mathematical calculation methods, including a method called round(), which is used to round floating-point numbers. This article explains how to correctly use the Math.round() method to round numbers and provides corresponding code examples. Specific steps: Import the Math class: At the beginning of the code, you need to import java.la

C++ program to round a number to n decimal places C++ program to round a number to n decimal places Sep 12, 2023 pm 05:13 PM

Representing numbers as output is an interesting and important task when writing a program in any language. For integer types (data of type short, long, or medium), it is easy to represent numbers as output. For floating point numbers (float or double type), sometimes we need to round them to a specific number of decimal places. For example, if we want to represent 52.24568 as three decimal places, some preprocessing is required. In this article, we will introduce several techniques to represent floating point numbers to a specific number of decimal places by rounding. Among the different approaches, it is important to use a C-like format string, use the precision argument, and use the round() function from the math library. Let’s look at them one by one. with

PHP floating point number rounding method PHP floating point number rounding method Mar 21, 2024 am 09:21 AM

This article will explain in detail the PHP floating point number rounding method. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Floating Point Rounding Overview Floating point numbers are represented in computers as a decimal point followed by an exponent, however, they are often stored in approximations with a limited number of digits. When you need to round a floating point number to a specific precision, there are several ways to do it. Method 1. round() function The round() function rounds a floating point number to the nearest integer. It accepts floating point numbers and optional precision parameters. For example: $num=1.55;echoround($num);//Output: 2echoround($num,1)

js rounding js rounding Jul 04, 2023 am 10:07 AM

js rounding methods: 1. tofixed method, which can round Number to a number with specified decimal places; 2. round() method, which can round a number to the nearest integer.

Round floating point numbers using Math.Round function in C# Round floating point numbers using Math.Round function in C# Nov 18, 2023 pm 02:17 PM

Using the Math.Round function in C# to round floating-point numbers requires specific code examples. In the C# programming language, sometimes we need to round floating-point numbers. At this time, we can use the Math.Round function to achieve this function. The Math.Round function is a built-in function in C# used for mathematical calculations. Its main function is to round the specified floating point number. The following is the common format of the Math.Round function: Math.Round(doub

How to round in oracle How to round in oracle Dec 13, 2023 am 10:28 AM

The ROUND function can be used in Oracle to perform rounding operations. The basic syntax of the ROUND function is "ROUND(number, decimal_places)". Among them, number is the number to be rounded, and decimal_places is the number of decimal places to be retained.

Round a floating point number to a specified precision using the math.Round function Round a floating point number to a specified precision using the math.Round function Jul 24, 2023 pm 11:22 PM

Use the math.Round function to round floating-point numbers to a specified precision. When performing floating-point calculations, we often need to round the results to a specified precision. The math package in the Go language provides the Round function, which can easily implement this function. This article will introduce how to use the math.Round function to round floating point numbers to a specified precision and provide code examples. The prototype of the math.Round function is as follows: funcRound(xfloat64)

See all articles