Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

string to accept two characters after point javascript

const translateString = (input) => {
  const [beforeDot, afterDot] = input.split('.')
  if (afterDot) {
    const afterDotTrimmed = afterDot.slice(0, 2)

    if (afterDotTrimmed === '00' || afterDotTrimmed === '0') {
      return beforeDot
    } else {
      return beforeDot + '.' + afterDotTrimmed
    }
  } else {
    return beforeDot
  }
}

translateString('14.0090'); // 14
translateString('14.0'); // 14
translateString('14.90'); // 14.90
translateString('14.96'); // 14.96
translateString('14.9'); // 14.9
translateString('14.'); // 14
translateString('14'); // 14
 
PREVIOUS NEXT
Tagged: #string #accept #characters #point #javascript
ADD COMMENT
Topic
Name
2+4 =