Home Web Front-end JS Tutorial js formatted amount optional with thousandths and preservation of precision_javascript skills

js formatted amount optional with thousandths and preservation of precision_javascript skills

May 16, 2016 pm 05:01 PM
Accuracy

js formatted amount, optional with or without thousandths, optional retention of precision, also found online, but no problem to use

Copy code The code is as follows:

/*
Round the value and format it.
@param num value (Number or String)
@param cent The decimal to be retained Bit (Number)
@param isThousand whether thousandth place is required 0: not required, 1: required (numeric type);
@return string in format, such as '1,234,567.45'
@type String
*/
function formatNumber(num,cent,isThousand){
num = num.toString().replace(/$|,/g,'');
if(isNaN(num)) //Check that the incoming value is a numeric type.
num = "0";
if(isNaN(cent))//Ensure that the incoming decimal place is a numeric value.
cent = 0;
cent = parseInt(cent);
cent = Math.abs(cent);//Find the number of decimal places and make sure it is a positive integer.
if(isNaN(isThousand))//Make sure whether the input is required Thousands is a numeric type.
isThousand = 0;
isThousand = parseInt(isThousand);
if(isThousand < 0)
isThousand = 0;
if(isThousand >= 1) //Make sure the value passed in is only 0 or 1
isThousand = 1;
sign = (num == (num = Math.abs(num))); //Get the sign (positive/negative number )
//Math.floor: Returns the largest integer less than or equal to its numerical parameter
num = Math.floor(num*Math.pow(10,cent) 0.50000000001);//Convert the specified decimal places first into an integer. Extra decimal places are rounded off.
cents = num%Math.pow(10,cent); //Find the decimal place value.
num = Math.floor(num/Math.pow(10, cent)).toString();//Find the integer digit value.
cents = cents.toString();//Convert the decimal digits into a string to find the length of the decimal digits.
while(cents. lengthcents = "0" cents;
}
if(isThousand == 0) //No thousandth place character required.
return (((sign)?'':'-') num '.' cents);
//Format the integer part into thousandths.
for (var i = 0; i < ; Math.floor((num.length-(1 i))/3); i )
num = num.substring(0,num.length-(4*i 3)) ','
num .substring(num.length-(4*i 3));
return (((sign)?'':'-') num '.' cents);
}
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 enable or disable enhanced pointer precision on Windows 11 How to enable or disable enhanced pointer precision on Windows 11 Sep 27, 2023 pm 12:21 PM

Pointer precision is crucial in situations where higher precision and better cursor positioning are required. It is enabled by default in Windows 11, but you may need to reconfigure enhanced pointer precision for better performance. For example, you might not want Windows to automatically re-adjust the pointer speed, but instead cover a fixed distance when making similar mouse movements. What is enhanced pointer precision? Enhanced pointer precision adjusts how far the cursor moves based on how fast the mouse is moving. Therefore, the faster the mouse moves, the greater the distance covered. For those wondering what Windows Enhanced Pointer Precision does, it changes mouse sensitivity. How to turn enhanced pointer precision on or off in Windows 11? 1. Press through Settings

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

As a popular server-side scripting language, PHP often encounters problems of loss of precision or calculation errors when performing floating-point calculations. These problems may affect the accuracy and stability of the program. This article will explore the causes of PHP floating point calculation errors, propose some avoidance strategies, and give specific code examples for reference. 1. Reasons for PHP floating-point calculation errors. In computers, floating-point numbers are represented in binary form, and binary cannot accurately represent all decimal decimals, which leads to the inaccuracy of floating-point numbers.

Discuss the accuracy of dividing by 10000 in PHP Discuss the accuracy of dividing by 10000 in PHP Mar 21, 2023 pm 03:49 PM

PHP is a widely used server-side programming language that can be embedded in HTML and used to develop web applications, command-line scripts, and desktop applications. In PHP, we often need to perform numerical calculations, and these calculations may encounter accuracy issues. This article will introduce the accuracy problem of dividing by 10000 in PHP and provide solutions.

Improve Golang program to solve accuracy problem Improve Golang program to solve accuracy problem Feb 24, 2024 pm 01:21 PM

Optimizing Golang programs and solving the problem of precision loss requires specific code examples. In the daily programming process, we often encounter the problem of precision loss, especially when using Golang for numerical calculations. These precision losses may be caused by floating point calculations, type conversions, etc., which have a certain impact on the accuracy and performance of the program. This article will introduce how to optimize Golang programs, solve the problem of precision loss, and provide specific code examples. 1. Use a high-precision calculation library if the program

The first post since the boss left! Stability official code model Stable Code Instruct 3B The first post since the boss left! Stability official code model Stable Code Instruct 3B Mar 29, 2024 pm 10:16 PM

After the boss left, the first model arrived! Just today, StabilityAI officially announced the new code model StableCodeInstruct3B. Picture stability is very important. The departure of the CEO has caused some troubles to StableDiffusion. There is something wrong with the investment company, and there may also be problems with your salary. However, the wind and rain outside the building are turbulent, but the laboratory remains motionless. Research should be done, discussions should be carried out, and models should be adjusted. The war in various fields of the large model is in decline. Not only is it spreading its stall to engage in all-out war, but every research is also making continuous progress. For example, today's StableCodeInstruct3B was built on the previous StableCo

PHP BCMath Overcomes Troubles: The Ultimate Blade for Solving Large Number Operations PHP BCMath Overcomes Troubles: The Ultimate Blade for Solving Large Number Operations Feb 23, 2024 am 09:58 AM

When dealing with large numbers in PHP, the BCMath library is your best choice. It provides a set of functions that help you easily perform various arithmetic operations without encountering precision or overflow issues. To use the BCMath library, you need to first use functions such as bcadd(), bcsub(), bcmul(), bcdiv(), and bcmod() to perform addition, subtraction, multiplication, division, and modulo operations on the numbers you want to operate on, e.g. :

In-depth analysis of PHP floating point operation precision distortion problem In-depth analysis of PHP floating point operation precision distortion problem Feb 27, 2024 pm 05:21 PM

As a scripting language, PHP is widely used in Web development, and its flexibility and ease of use are favored by many developers. However, when using PHP for floating-point operations, precision distortion is often encountered, which brings certain challenges to the development process. This article will conduct an in-depth analysis of the accuracy distortion problem of PHP floating point operations and provide specific code examples. 1. Problem Description Floating point numbers are a data type that represents decimals. However, because the computer uses binary to represent floating point numbers internally, most decimals cannot be accurately expressed in binary.

How to solve the problem of floating point calculation accuracy in Python? How to solve the problem of floating point calculation accuracy in Python? Oct 19, 2023 am 11:22 AM

How to solve the problem of floating point calculation accuracy in Python? In computer science, floating-point calculation accuracy is one of the common challenges. Since computers use limited bits internally to represent floating-point numbers, there may be a loss of precision when representing and operating certain decimals. Python, as a powerful programming language, provides some methods to solve this problem. A common way to solve the problem of precision in floating point calculations is to use the Decimal class. Decimal class provides higher precision

See all articles