I'm working with an API that returns nothing but strings in the response. I need to format any decimal value returned in the string to have a leading zero but no trailing zeros. If the value is anything other than a float in the string it should be returned with out any formatting changes.
Example: if the value is ".7", ".70" or "0.70" my function will always return "0.7". If the value is "1+" it will return "1+".
Initially I thought the API was returning floats so I was doing this below. The places param is how many decimal places to display.
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 accomplish what the above function is doing when the value is a decimal string, but just return the inputed value if the string does not contain a float? As an aside I'm using Angular and will end up making this a filter.