Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove the last two decimal places from javascript

Shulou Source: shulou.com Published: 2022-06-01 17:28:06 09月28日 Update

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!

Tags: Decimals decimal points objects numeric values content articles statements characters strings methods learning help tutorials data understandability more organization regularity environment computer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information NVidia Docker Linux Apple