Home Web Front-end JS Tutorial js floating point numbers retain two decimal points sample code (rounding)_javascript skills

js floating point numbers retain two decimal points sample code (rounding)_javascript skills

May 16, 2016 pm 05:07 PM
rounding floating point number

Copy code The code is as follows:

var changeTwoDecimal_f= function (floatvar){
var f_x = parseFloat (floatvar);
if (isNaN(f_x)){
return '0.00';
}
var f_x = Math.round(f_x*100)/100;
var s_x = f_x.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0){
pos_decimal = s_x.length;
s_x = '.';
}
while (s_x.length <= pos_decimal 2){
s_x = '0';
}
return s_x;
}

Rounding function provided by js:
Copy code The code is as follows:

js rounding function toFixed( ), the parameter inside is the number of decimal places to retain.

<script language="javascript">
document.write("<h1>JS example of retaining two decimal places</h1><br>");
var a=2.1512131231231321;
document.write("Original value:" a "<br>");
document.write("Two decimal points:" a.toFixed(2) "<br> Four decimal points" a.toFixed(4));
</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 Article

Hot Article

Hot Article Tags

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)

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

PHP floating point number rounding method

How to convert string to decimal in php How to convert string to decimal in php Mar 22, 2023 pm 03:22 PM

How to convert string to decimal in php

Convert floating point number to string using strconv.FormatFloat function Convert floating point number to string using strconv.FormatFloat function Jul 25, 2023 am 11:45 AM

Convert floating point number to string using strconv.FormatFloat function

Causes and avoidance strategies of PHP floating point calculation errors Causes and avoidance strategies of PHP floating point calculation errors Feb 27, 2024 pm 06:33 PM

Causes and avoidance strategies of PHP floating point calculation errors

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

C++ program to round a number to n 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?

An in-depth explanation of PHP BCMath: Unleashing the potential of number operations An in-depth explanation of PHP BCMath: Unleashing the potential of number operations Feb 23, 2024 am 09:10 AM

An in-depth explanation of PHP BCMath: Unleashing the potential of number operations

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

js rounding

See all articles