Is there a way to truncate scientific notation numbers in Javascript ?

As you all know since it is one of the most asked topic on SO, I am having problems with rounding errors (it isn't actually errors, I am well aware). Instead of explaining my point, I'll give an example of what possible numbers I have and which input I want to be able to obtain: Let's say var a = 15 * 1e-9; alert(a) outputs 1.5000000000000002e-8 I want to be able to obtain `1.5e-8` instead, but I cannot just multiply by 10e8, round and divide by 10e8 because I don't know if it will be e-8 or e-45 or anything else. So basically I want to be able to obtain the `1.5000002` part, apply `toFixed(3)` and put back the exponent part. I could convert into a string and parse but it just doesn't seem right... Any idea ? ---------------------- (I apologize in advance if you feel this is one of many duplicates, but I could not find a similar question, only related ones) Gael

以上就是Is there a way to truncate scientific notation numbers in Javascript ?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏