Last active
April 29, 2016 07:52
-
-
Save sidisinsane/c1837dbc21edad277957c00903e00590 to your computer and use it in GitHub Desktop.
Revokes formatting previously applied by ‘Intl.NumberFormat‘
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --- | |
// Revokes formatting previously applied by ‘Intl.NumberFormat‘ | |
// | |
// Usage: | |
// '123.456,78'.revokeIntlNumberFormat('de-DE'); | |
// Result: | |
// 123456.78 | |
// --- | |
Object.defineProperty(Object.prototype, 'revokeIntlNumberFormat', { | |
value: function(locale) { | |
if (typeof Intl.NumberFormat === 'function' && typeof locale !== 'undefined') { | |
var tsdSep = new Intl.NumberFormat(locale, { | |
useGrouping: true, | |
maximumFractionDigits: 0 | |
}).format(1111).replace(/[0-9]/g, ''); | |
var decSep = new Intl.NumberFormat(locale, { | |
minimumFractionDigits: 1 | |
}).format(1.1).replace(/[0-9]/g, ''); | |
return Number(this.replace(tsdSep, '').replace(decSep, '.')); | |
} else { | |
return this; | |
} | |
}, | |
enumerable: false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment