I have a page with a grid where user numbers are saved. It has the following pattern: each number ends with 3 digits after the decimal point. It doesn’t look very good when, for example, user input
123,450
123,670
123,890
It is much better to have only 2 decimal places, because the latter is 0completely meaningless and superfluous.
Until now, it should have 3 digits only if at least one element in the array does not end 0
For instance:
123,455
123,450
123,560
In this case, the 1st element of the array has the last digit that is not equal 0, and therefore, all elements must have 3 digits. The same story with 2 or 1 zeros
Zeros are redundant:
123,30
123,40
123,50
Need zeros:
123,35
123,40
123,50
The question is, how can I implement it programmatically? I started like this:
var zeros2Remove = 0;
numInArray.forEach(function(item, index, numInArray)
{
var threeDigitsAfterComma = item.substring(item.indexOf(',') + 1);
for(var j = 2; j <= 0; j--)
{
if(threeDigitsAfterComma[j] == 0)
{
zeros2Remove =+ 1;
}
else
}
})
, , , , , 1 , . , , , ...