0

I am comparing two strings with a currency that seems to be the same. One is created with toLocaleString() and the other by declaring a static string. Both put out the same value on the console, but every compare method fails for them.

Any idea why this is not working? This is blowing my mind! I am not sure if it is the euro symbol...

const localStringValue = (2).toLocaleString('de', {style: 'currency', maximumFractionDigits: 2, currency: 'EUR'});
const stringValue = '2,00 €';

console.log('local string: ', localStringValue);
console.log('string: ', stringValue);

console.log('strict compare', localStringValue === stringValue);
console.log('compare', localStringValue == stringValue);
console.log('locale compare', localStringValue.localeCompare(stringValue));
Franki1986
  • 1,320
  • 1
  • 15
  • 40

1 Answers1

3

Value and currency is separated by non-breaking space in localStringValue, but in stringValue it's normal space.

const localStringValue = (2).toLocaleString('de', {
  style: 'currency',
  maximumFractionDigits: 2,
  currency: 'EUR'
});
const stringValue = '2,00 €';

console.log('local string: ', localStringValue, encodeURIComponent(localStringValue));
console.log('      string: ', stringValue, encodeURIComponent(stringValue));
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • Oh man.. I spent 3h on this and thought it would be the euro symbol, because with GBP format it worked ('£2.00')! But so it is the space/nbs that hits it!! Thank you so much!!! <3 – Franki1986 Oct 05 '21 at 14:35
  • Do you have a link to the Information you gave? – Franki1986 Oct 05 '21 at 19:41
  • @Franki1986 according to the specification this function is implementation-defined https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-number.prototype.tolocalestring – ponury-kostek Oct 11 '21 at 18:06