Dont round on trailing zeros

Hello, so for my application I am wanting to round every number passed in the function to return the nearest tenth of a decimal. However, in my application if I give it a whole number, it doesn’t work with whole numbers with no decimal.
Here is the function:
export function toNearestTenth(num: number) {

return (Math.round((num + Number.EPSILON) * 100) / 100)

For example: if I have the number 315 and it will just return 315, not 315.00

If you want to enforce trailing decimal zeroes, you’re going to have to convert it to a string, either when it is stored, or when it is displayed.

1 Like

Okay, thanks. So there’s no way to have a decimal number in javascript with trailing zeros?

Nope trailing (or leading) zeros are a display concern, so that data isn’t associated with the primitive number.

You could in theory create your own class that stored that kind of information and had its own toString method that rendered based on number of set number of decimal places. But out of the box? No.

Okay thank you both. I appreciate yall and thank you for your time!