I need to work on formatting decimal values returned by an API that only responds with strings. The requirement is to add a leading zero but no trailing zeros to any decimal value in the string. If the value is not a float, it should remain unchanged.
For example, if the input is ".7", ".70", or "0.70", my function should always return "0.7". Similarly, for inputs like "1+", it should just return "1+".
Initially, I had assumed that the API was sending floats, so I used the following approach considering the number of decimal places required as a parameter:
function setDecimalPlace(input, places) {
if (isNaN(input)) return input;
var factor = "1" + Array(+(places > 0 && places + 1)).join("0");
return Math.round(input * factor) / factor;
};
How can I modify the above function to achieve the same result when the input is a decimal string, while returning the original value if it's not a float? Additionally, I am working with Angular and plan to convert this into a filter.