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

javascript - Function Number.prototype.toLocaleString() returns negative 0 - Stack Overflow

programmeradmin4浏览0评论

Below code returns negative 0 instead of 0. Is there a way to fix this issue?

const x=-0.01;
console.log(
  x.toLocaleString('en-US',{minimumFractionDigits:0,maximumFractionDigits:0})
)

Below code returns negative 0 instead of 0. Is there a way to fix this issue?

const x=-0.01;
console.log(
  x.toLocaleString('en-US',{minimumFractionDigits:0,maximumFractionDigits:0})
)

Share Improve this question edited Jan 20 at 9:26 mplungjan 178k28 gold badges180 silver badges240 bronze badges asked Jan 20 at 8:48 Chinnmay B SChinnmay B S 1151 silver badge6 bronze badges 3
  • Please provide all relevant code when asking a question. We don't know your input for x and can't assume it for you. We don't know your expected output either. – icecub Commented Jan 20 at 8:58
  • In my browers, running the code you provide, the result is '0'.Please provide more information – jerry-lzj Commented Jan 20 at 9:03
  • @icecub I have updated the value of x now. – Chinnmay B S Commented Jan 20 at 9:11
Add a comment  | 

1 Answer 1

Reset to default 5

You can set the signDisplay option to exceptZero or negative:

const x = -0.01;
const y = x.toLocaleString('en-US',{
  minimumFractionDigits: 0,
  maximumFractionDigits: 0,
  signDisplay: 'exceptZero'
});

console.log(y);

Note that using exceptZero will also add an explicit sign for positive numbers, while using negative will not.

发布评论

评论列表(0)

  1. 暂无评论