6 examples of 'allow only two digits after decimal using javascript' in JavaScript

Every line of 'allow only two digits after decimal using javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
311function convertDecimal(value, surroundDecimalsWith) {
312 if (/^-?([0]|([1-9][0-9]*))(\.[0-9]+)?$/.test(value) == false) {
313 throw new Error("value supposed to be a decimal number but got: " + value);
314 }
315 if (surroundDecimalsWith) {
316 return {
317 value: value,
318 toJSON: function () {
319 return surroundDecimalsWith.str + value + surroundDecimalsWith.str;
320 }
321 };
322 }
323 else {
324 return value;
325 }
326}
7export default function formatDecimal (value: string): string {
8 // We can do this by adjusting the regx, however for the sake of clarity
9 // we rather strip and re-add the negative sign in the output
10 const isNegative = value[0].startsWith('-');
11 const matched = isNegative
12 ? value.substr(1).match(NUMBER_REGEX)
13 : value.match(NUMBER_REGEX);
14
15 return matched
16 ? `${isNegative ? '-' : ''}${matched.join(',')}`
17 : value;
18}
81static parse(value) {
82 return value;
83}
115function formatDecimal(valueAsNumber) {
116 if (!hasDecimalPlaces(valueAsNumber)) {
117 return Math.round(valueAsNumber).toString();
118 }
119 let text = valueAsNumber.toFixed(1);
120 return text.endsWith('.0') ? Math.round(valueAsNumber).toString() : text;
121}
8function getDecimal(format: Format) {
9 switch (format) {
10 case 'tz':
11 return TZ_DECIMALS;
12 case 'mtz':
13 return MTZ_DECIMALS;
14 case 'mutez':
15 default:
16 return 0;
17 }
18}
119function getDigits() {
120 return DG.PlotUtilities.findFractionDigitsForRange(iAttrDesc.getPath('attributeStats.minMax'));
121}

Related snippets