* Round the input value using the rounding method defined in the settings. * This function accepts multiple rounding methods. See the documentation for more details about those. * * Note : This is handled as text since JavaScript math functions can return inaccurate values. *
(inputValue, settings, decimalPlacesToRoundTo)
| 5790 | * @returns {*} |
| 5791 | */ |
| 5792 | static _roundValue(inputValue, settings, decimalPlacesToRoundTo) { |
| 5793 | //XXX Note; this function is static since we need to pass a `settings` object when calling the static `AutoNumeric.format()` method |
| 5794 | if (AutoNumericHelper.isNull(inputValue)) { |
| 5795 | // Prevent rounding a `null` value |
| 5796 | return inputValue; |
| 5797 | } |
| 5798 | |
| 5799 | //TODO Divide this function to make it easier to understand |
| 5800 | inputValue = (inputValue === '') ? '0' : inputValue.toString(); |
| 5801 | if (settings.roundingMethod === AutoNumeric.options.roundingMethod.toNearest05 || |
| 5802 | settings.roundingMethod === AutoNumeric.options.roundingMethod.toNearest05Alt || |
| 5803 | settings.roundingMethod === AutoNumeric.options.roundingMethod.upToNext05 || |
| 5804 | settings.roundingMethod === AutoNumeric.options.roundingMethod.downToNext05) { |
| 5805 | return this._roundCloseTo05(inputValue, settings); |
| 5806 | } |
| 5807 | |
| 5808 | const [negativeSign, preparedValue] = AutoNumeric._prepareValueForRounding(inputValue, settings); |
| 5809 | inputValue = preparedValue; |
| 5810 | |
| 5811 | const decimalCharacterPosition = inputValue.lastIndexOf('.'); |
| 5812 | const inputValueHasNoDot = decimalCharacterPosition === -1; // No dot character is found in the `inputValue` |
| 5813 | const [integerPart, decimalPart] = inputValue.split('.'); // Here the decimal character is always a period '.' |
| 5814 | const hasDecimals = decimalPart > 0; |
| 5815 | |
| 5816 | // If no decimals are detected |
| 5817 | if (!hasDecimals && |
| 5818 | (settings.allowDecimalPadding === AutoNumeric.options.allowDecimalPadding.never || |
| 5819 | settings.allowDecimalPadding === AutoNumeric.options.allowDecimalPadding.floats)) { |
| 5820 | // If the value decimalPart is only one or more zeroes, then it needs to be removed from the resulting string (cf. issue #652) |
| 5821 | return (Number(inputValue) === 0) ? integerPart : `${negativeSign}${integerPart}`; |
| 5822 | } |
| 5823 | |
| 5824 | // Else there are some decimal places that may need to be rounded |
| 5825 | // Sets the truncate zero method |
| 5826 | let temporaryDecimalPlacesOverride; |
| 5827 | if (settings.allowDecimalPadding === AutoNumeric.options.allowDecimalPadding.always || |
| 5828 | settings.allowDecimalPadding === AutoNumeric.options.allowDecimalPadding.floats) { |
| 5829 | temporaryDecimalPlacesOverride = decimalPlacesToRoundTo; |
| 5830 | } else if (settings.allowDecimalPadding > 0) { |
| 5831 | temporaryDecimalPlacesOverride = settings.allowDecimalPadding; |
| 5832 | } else { |
| 5833 | temporaryDecimalPlacesOverride = 0; |
| 5834 | } |
| 5835 | |
| 5836 | // Define the decimal position to use (use the very last position if there are no dot in the initial inputValue) |
| 5837 | const decimalPositionToUse = inputValueHasNoDot ? inputValue.length - 1 : decimalCharacterPosition; |
| 5838 | // Checks decimal places to determine if rounding is required |
| 5839 | let checkDecimalPlaces = (inputValue.length - 1) - decimalPositionToUse; |
| 5840 | let inputValueRounded = ''; |
| 5841 | |
| 5842 | // Check if no rounding is required |
| 5843 | if (checkDecimalPlaces <= decimalPlacesToRoundTo) { |
| 5844 | // Check if we need to pad with zeros |
| 5845 | inputValueRounded = inputValue; |
| 5846 | if (checkDecimalPlaces < temporaryDecimalPlacesOverride) { |
| 5847 | if (inputValueHasNoDot) { |
| 5848 | inputValueRounded = `${inputValueRounded}${settings.decimalCharacter}`; |
| 5849 | } |
no test coverage detected