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
Two Point Museum: Bungle Wasteland Location Guide
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot tools Tags

Hot Article
Two Point Museum: Bungle Wasteland Location Guide
4 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks ago
By DDD
Repo: How To Revive Teammates
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

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

PHP floating point number rounding method

How to convert string to decimal in php

Convert floating point number to string using strconv.FormatFloat function

Causes and avoidance strategies of PHP floating point calculation errors

C++ program to round a number to n decimal places

How to round a number using Math.round() method in Java?

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