Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove the last two decimal places from javascript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how javascript removes the last two decimal places". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to remove the last two decimal places in javascript".

Methods: 1, use "numeric string object .substring (0, numeric string object .indexOf (". ") + 3)" statement; 2, use "Math.round (numeric object * 100) / 100" statement; 3, use "numeric object .tofixed (2)" statement.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

How does javascript remove the last two decimal places? What should I do if I take 22.123456 into 22.12?

1. Intercept via substring

Function getnum () {var num = 22.123456 X var s=num.toString (); var result = s.substring (0mens.indexOf (".") + 3); alert (result);} getnum ()

2. Regular expression

Function getnum () {var num = 22.123456 aNew str=num.toString (); var aNew;var re = / ([0-9] +\. [0-9] {2}) [0-9] * /; aNew = str.replace (re, "$1"); alert (aNew);} getnum ()

3. Data type retention

Function getnum () {var num=22.123456;alert (Math.round (num*100) / 100);} getnum ()

4. ToFixed method

Var num=22.123456;alert (num.toFixed (2)); these are all the contents of the article "how to remove the last two decimal places in javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report