在javascript中尾随零 [英] Trailing zeros in javascript

查看:81
本文介绍了在javascript中尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在javascript中为一个数字添加尾随零?

Is there a way to add trailing zeros to a number in javascript?

例如:
47.0而不是47
或0.0而不是0

For example: 47.0 instead of 47 or 0.0 instead of 0

当我在json中返回数字时,零点消失器。

when I return the numbers in a json, the zeros disapper.

我需要它们是数字而不是字符串所以.toFixed(2)没有帮助。

I need them to be numbers and not strings so .toFixed(2) doesn't help.

谢谢!

推荐答案

没有。你想要的是一个特定的表示,大多数简单的类型没有概念。

No. What you want is a specific presentation, which most simple types has no concept of.

无论如何,当序列化为JSON请求或响应时,它仍然会被转换为字符串,但如果接收端表示为46.0,46.00或46.000,除非它周围有,否则接收端根本不在乎。

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.

这篇关于在javascript中尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆