最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Trailing zeros in javascript - Stack Overflow

programmeradmin2浏览0评论

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

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

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

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

Thanks!

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

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

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

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

Thanks!

Share Improve this question asked Nov 12, 2013 at 19:19 GilGil 632 silver badges8 bronze badges 2
  • 2 Numbers (aka IEEE754 Doubles) are represented in binary exponential notation. They can not have trailing (or leading... or whatever) zeroes. – Vatev Commented Nov 12, 2013 at 19:23
  • So you take the string and convert them to numbers when you need to perform an operation with them. – epascarello Commented Nov 12, 2013 at 19:42
Add a ment  | 

2 Answers 2

Reset to default 4

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.

The number 47.0 is the same number as 47, so the only way to change its representation is to make it a string.

发布评论

评论列表(0)

  1. 暂无评论