precision.js 748 B

12345678910111213141516171819202122
  1. var plugin = {
  2. level1: {
  3. value: function precision(_name, value, options) {
  4. if (!options.precision.enabled || value.indexOf('.') === -1) {
  5. return value;
  6. }
  7. return value
  8. .replace(options.precision.decimalPointMatcher, '$1$2$3')
  9. .replace(options.precision.zeroMatcher, function(match, integerPart, fractionPart, unit) {
  10. var multiplier = options.precision.units[unit].multiplier;
  11. var parsedInteger = parseInt(integerPart);
  12. var integer = Number.isNaN(parsedInteger) ? 0 : parsedInteger;
  13. var fraction = parseFloat(fractionPart);
  14. return Math.round((integer + fraction) * multiplier) / multiplier + unit;
  15. });
  16. }
  17. }
  18. };
  19. module.exports = plugin;