code:
const toShow = (money, format = 'BRL') => {
const options = { style: 'currency', currency: format };
const numberFormat = new Intl.NumberFormat('pt-BR', options);
return numberFormat.format(money);
};
console.log([...toShow(1001)].map(f => f.charCodeAt(0)))
console.log([...'R$ 1,001.00'].map(f => f.charCodeAt(0)))
console.log(toShow(1001) === 'R$ 1,001.00')
output:
~/Projects/gtkatakura/test $ node intl-format.js
[ 82, 36, 160, 49, 44, 48, 48, 49, 46, 48, 48 ]
[ 82, 36, 32, 49, 44, 48, 48, 49, 46, 48, 48 ]
false
This is what I get in the Chrome Canary and Node.js fwith full-icu:
[82, 36, 160, 49, 46, 48, 48, 49, 44, 48, 48]
[82, 36, 32, 49, 44, 48, 48, 49, 46, 48, 48]
false
@enieber I guess we both know this code 馃
Yeah. Node.js fwith full-icu returns 'R$ 1.001,00' (the point and the comma are correct, in this case).
The problem is in space, which is using charCode 160, instead of 32.
Is that correct? Remember that according to the unicode table:
160 = NO-BREAK SPACE
32 = SPACE
A non-breaking space between $ and the 1 seems correct to me. Is it not? Isn't that a space that simply prevents a line break like this?:
Some text that gets you to the point where things might wrap to the next line and then R$
1.001,00.
Non-breaking space would make that render like this instead:
Some text that gets you to the point where things might wrap to the next line and then
R$ 1.001,00.
Or am I wrong?
Hm, makes sense. However, I think that methods like #format should document this use case. I have already seen several cases of people trying to make a comparison between strings have this problem (comparing text), and lost time until they notice that the problem is in charCode.
Hm, makes sense. However, I think that methods like #format should document this use case. I have already seen several cases of people trying to make a comparison between strings have this problem (comparing text), and lost time until they notice that the problem is in charCode.
If I'm not mistaken, then for the most part, that method isn't documented by Node.js. (I mean, maybe a little bit at https://nodejs.org/api/intl.html but it's mostly incidental.) It's documented in JavaScript reference material like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/format. In fact, the demo code and the very first example under _Examples_ on that page results in a non-breaking space. There's an _Edit_ button in the upper right on the page. Maybe consider adding an appropriate note or warning there?
I'm going to close this as it's not a bug, but definitely feel free to continue to comment, especially if you think it's incorrect to close this and it should be re-opened.
Most helpful comment
A non-breaking space between
$and the1seems correct to me. Is it not? Isn't that a space that simply prevents a line break like this?:Non-breaking space would make that render like this instead:
Or am I wrong?