Function to Round Off Values to One Decimal Place with Precision

It is crucial to handle numerical values with precision, especially when dealing with calculations or financial data. One common requirement is to round off a value to one decimal place and ensure that there is always a trailing zero for added precision.

function roundToOneDecimalPlaces(context, fieldName) {

  // Access the current record in the context

  var currentRecord = context.currentRecord;

  // Retrieve the original value from the specified field

  var value = currentRecord.getValue({

    fieldId: fieldName

  });

  // Round off to one decimal place

  var roundedValue = Math.round(value * 10) / 10;

  // Convert the rounded value to a string

  var stringValue = roundedValue.toString();

  // Add zero at the end if there is no digit after the decimal point

  if (stringValue.indexOf(‘.’) === -1) {

    stringValue += ‘.0’;

  }

  // Add zero at the beginning if the value is between 1 and 0

  if (roundedValue < 1 && roundedValue > 0) {

    stringValue = ‘0’ + stringValue;

  }

  // Set the modified value back to the current record

  currentRecord.setValue({

    fieldId: fieldName,

    value: stringValue

  });

}

Leave a comment

Your email address will not be published. Required fields are marked *