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
|
1 Answer
Reset to default 5You 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.
x
and can't assume it for you. We don't know your expected output either. – icecub Commented Jan 20 at 8:58