Home > database >  toLocalString not the same as string when comparing
toLocalString not the same as string when comparing

Time:10-06

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));

CodePudding user response:

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));

  • Related