简体   繁体   English

JavaScript中的尾随零

[英]Trailing zeros in javascript

Is there a way to add trailing zeros to a number in javascript? 有没有办法将尾随零添加到javascript中的数字?

For example: 47.0 instead of 47 or 0.0 instead of 0 例如:47.0而不是47或0.0而不是0

when I return the numbers in a json, the zeros disapper. 当我在json中返回数字时,则为零。

I need them to be numbers and not strings so .toFixed(2) doesn't help. 我需要它们是数字而不是字符串,因此.toFixed(2)不能帮助您。

Thanks! 谢谢!

数字47.0与47是相同的数字,因此更改其表示形式的唯一方法是使其成为字符串。

No. What you want is a specific presentation, which most simple types has no concept of. 不,您想要的是一个特定的演示文稿,大多数简单类型都没有这个概念。

Anyway, when serialized into a JSON request or response it would still be converted to a string, but the receiving end shouldn't care at all if it's represented as 46.0, 46.00 or 46.000 unless it has the "" around it. 无论如何,当序列化为JSON请求或响应时,它仍将转换为字符串,但是接收端根本不需要关心它是否表示为46.0、46.00或46.000,除非它周围带有“”。

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

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