In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how javascript implements division and retains two decimal places. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Implementation: 1, use the "/" operator for division, syntax "numeric 1 / numeric 2"; 2, use the "result of division operation .tofixed (2)" or "Math.floor (result of division operation * 100) / 100" statement to retain two decimal places.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Javascript implements division to retain two decimal places
It can be divided into two parts:
Division of two numbers: division can be performed using the "/" operator
Keep two decimal places
In general, there are three ways to retain two decimal places:
1. Directly use the method toFixed (n) that comes with the numeric type, where the parameter is the number of digits to be reserved, and the result of this conversion is the character type.
Var Apocalypse 10 positionvar baccalaure3 var canals: + c.toFixed (2)
2, combined with Math.floor (), use the method of multiplication and division, for example, to keep two decimal places, Math.floor (clocked 100) / 100. if you want to keep three decimal places, it is Math.floor (clocked 1000) / 1000.
Var console.log ("keep two decimal places:" + Math.floor / 1000); console.log ("keep three decimal places:" + Math.floor (clocked 1000) / 1000)
This method of multiplication and division is the most common method.
3. Use regular expressions to intercept. There is a need to test the skills of the rules.
Var ("result of division by two numbers:" + c); console.log ("keep two decimal places:" + c.toString (). Match (new RegExp (/ ^\ d + (?:\.\ d {0jin2})? /)
Thank you for reading! This is the end of this article on "how to divide and retain two decimal places in javascript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.