简体   繁体   English

尾随点是否在Javascript中所有值均为零

[英]Trailing if after dot all values are zeros in Javascript

I want to remove 0s after dot 我想在点后删除0

I'm using SQL with Laravel, in database the prices are stored using decimal(10,3) [I need 3 decimals]. 我在Laravel中使用SQL,在数据库中,价格使用小数(10,3)[我需要3个小数]存储。

From database I'm getting 从数据库中我得到

400.000 400.000
451.500 451.500
1.522 1.522
20.000 20.000
300.210 300.210
143.100 143.100

I've tried to do parseFloat and toFixed(3), but it's not working how I'm expecting. 我曾尝试做parseFloat和toFixed(3),但是按我的期望却没有用。

I expect the output to be: 我希望输出为:

400 400
451.500 451.500
1.522 1.522
20 20
300.210 300.210
143.100 143.100

The values are not from an array. 这些值不是来自数组。 I'm getting the value from an AJAX response. 我从AJAX响应中获取价值。 I'm clicking on a button and I'm getting 400.000, clicking again.. I'm getting in response 451.500, etc. 我单击一个按钮,得到400.000,再次单击。.我得到451.500的响应,依此类推。

Using Array#filter and String#replace. 使用Array#filter和String#replace。 Works by matching only 0's after the dot. 通过在点后仅匹配0来工作。

/\\.0+$/ is simple regex. /\\.0+$/是简单的正则表达式。 It can be broken down into a few different parts. 它可以分为几个不同的部分。 The outer / s (slashes) indicate that whatever within it is the regex. 外面的/ s(斜杠)表示其中的任何内容都是正则表达式。

The important part is actually, \\.0+$ . 实际上,重要的部分是\\.0+$ By default, . 默认情况下, . will match any character, but in this scenario we just want to find the numbers that contain a dot. 可以匹配任何字符,但是在这种情况下,我们只想查找包含点的数字。 So we escape it by using \\. 因此,我们使用\\.对其进行escape \\. .

Once, we have the numbers that contain a dot, we need to see if they contain one or more 0s and only 0s. 一次,我们有了一个包含点的数字,我们需要查看它们是否包含一个或多个0并且仅包含0。

0+ will match at least one or more 0s. 0+至少匹配一个或多个0。

$ ensures that the number ends in a 0. $确保数字以0结尾。

tldr; tldr; Essentially. 实质上。 \\.0+$ will match anything that contains a . \\.0+$将匹配任何包含. and has at least one 0 trailing and ends in a 0. 并且至少有一个0尾随且以0结尾。

 const data = ['123.001','400.000', '451.500', '1.520', '20.000', '300.210', '143.100'] const res = data.map(n=>{ return n.replace(/\\.0+$/, ""); }); console.log(res); 

For simplicity sake, I turned your values into an array, however how you iterate over the values is up to you; 为简单起见,我将您的值转换为数组,但是如何迭代这些值由您决定; here's the concept: 这是概念:

 let arr = [400.000, 451.500, 1.522, 20.000, 300.210, 143.100] for(let x = 0; x < arr.length; x++) { if(arr[x] % 1 == 0) { arr[x] = arr[x].toFixed(0); } else { arr[x] = arr[x].toFixed(3); } console.log(arr[x]) } 

What this does is checks the value to see if it is divisible by 1 and returns without any decimal places, otherwise, it lets it round to 3 decimal places. 这样做是检查该值以查看它是否可以被1整除并返回没有小数位的位置,否则,将其舍入到小数点后3位。

Hope this helped! 希望这对您有所帮助!

You can use regex to check if the all the digits after \\. 您可以使用正则表达式检查\\.后面的所有数字\\. are zero or not. 是否为零。

If zero than replace with empty string else keep as it is. 如果为零,则用空字符串替换,否则保持原样。

 const data = ['400.000', '451.500', '1.520', '20.000', '300.210', '143.001'] const res = data.map(n=>{ return n.replace(/\\.0+$/, ""); }); console.log(res); 

 var number = 1.2345678; var result_number = round(number, 3); console.log(result_number); function round(value, decimals) { return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals); } 

Try this, and it will solve your issue :) 试试这个,它将解决您的问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM