简体   繁体   English

如何将字符串解析为 integer 并保留小数位(如果它们为零)?

[英]How can I parse a string as an integer and keep decimal places if they are zeros?

I have these strings: "59.50" & "30.00"我有这些字符串:“59.50”和“30.00”

What I need to do is convert them to integers but keep the trailing zeros at the end to effectively return:我需要做的是将它们转换为整数,但在末尾保留尾随零以有效返回:

59.50
30.00

I've tried:我试过了:

Math.round(59.50 * 1000) / 1000
Math.round(30.00 * 1000) / 1000

but ended up with但结束了

59.5
30

I'm assuming I need to use a different method than Math.round as this automatically chops off trailing zeros.我假设我需要使用与 Math.round 不同的方法,因为它会自动切断尾随零。

I need to keep these as integers as they need to be multiplied with other integers and keep two decimals points.我需要将它们保留为整数,因为它们需要与其他整数相乘并保留两位小数。 T thought this would be fairly straight forward but after a lot of searching I can't seem to find a solution to exactly what I need.我认为这会相当简单,但经过大量搜索后,我似乎无法找到完全符合我需要的解决方案。

Thanks!谢谢!

Your premise is flawed.你的前提是有缺陷的。 If you parse a number, you are converting it to its numerical representation, which by definition doesn't have trailing zeros.如果你解析一个数字,你就是把它转换成它的数字表示,根据定义,它没有尾随零。

A further flaw is that you seem to think you can multiply two numbers together and keep the same number of decimal places as the original numbers.另一个缺陷是您似乎认为可以将两个数字相乘并保持与原始数字相同的小数位数。 That barely makes sense.这几乎没有意义。

It sounds like this might be an XY Problem, and what you really want to do is just have two decimal places in your result.听起来这可能是一个 XY 问题,而您真正想要做的只是在结果中保留两位小数。

If so, you can use .toFixed() for this:如果是这样,您可以使用.toFixed()

 var num = parseFloat("59.50"); var num2 = parseFloat("12.33"); var num3 = num * num2 console.log(num3.toFixed(2)); // 733.64

Whenever you want to display the value of the variable, use Number.prototype.toFixed() .每当您想显示变量的值时,请使用Number.prototype.toFixed() This function takes one argument: the number of decimal places to keep.此函数采用一个参数:要保留的小数位数。 It returns a string, so do it right before viewing the value to the user.它返回一个字符串,因此请在向用户查看值之前执行此操作。

console.log((123.4567).toFixed(2)); // logs "123.46" (rounded)

保留小数 - 将字符串乘以 1 示例:"33.01" * 1 // 等于 33.01

Seems you are trying to retain the same floating point, so better solution will be some thing like似乎您正在尝试保留相同的浮点数,因此更好的解决方案是

parseFloat(string).toFixed(string.split('.')[1].length);

If you want numbers with decimal points, you are not talking about integers (which are whole numbers) but floating point numbers.如果你想要带小数点的数字,你不是在谈论整数(整数)而是浮点数。

In Javascript all numbers are represented as floating point numbers.在 Javascript 中,所有数字都表示为浮点数。

You don't need the trailing zeros to do calculations.您不需要尾随零来进行计算。 As long as you've got all the significant digits, you're fine.只要你有所有的有效数字,你就没事。

If you want to output your result with a given number of decimal values, you can use the toFixed method to transform your number into a formatted string:如果要使用给定数量的十进制值输出结果,可以使用 toFixed 方法将数字转换为格式化字符串:

var num = 1.5
var output = num.toFixed(2) // '1.50'
// the number is rounded
num = 1.234
output = num.toFixed(2) // '1.23'
num = 1.567
output = num.toFixed(2) // '1.57'

Here's a more detailed description of toFixed: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed下面是对 toFixed 的更详细描述: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

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

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