# Truncate decimals

Hi there, how can I truncate the decimals off a calculation? e.g. [[(Now.valueOf() - Date.parse(‘12/31/2021’).valueOf()) / 1000 / 60 / 60 / 24]]

You can wrap your whole expression with the Math.floor() function, where “floor” rounds down to the nearest whole integer. Using your example, it would look like:

[[ Math.floor((Now.valueOf() - Date.parse(‘12/31/2021’).valueOf()) / 1000 / 60 / 60 / 24) ]]

*Note that Math.floor() always rounds down which may not produce a result you expect or need with negative values. So in your example, with today’s date I get a value of -318.5169… (the date 12/31/2021 has not yet happened, so there are -318 days (+ some hours) “since” 12/31/2021). If a “floor function” is applied to -318.5 it yields -319. You may logically need “the nearest day” rather than strictly “rounding down” or “truncating decimals”. When I did something similar to this before, it took awhile to debug why “future date calculations” (i.e., negative values) were always off by one, but “past date calculations” (positive values) were correct. I ended up using a case statement testing if “date value” was less than 0 --and subtracting 1 to the value before (or after) “flooring” it. Alternatively, I could have used .toFixed() for the negative values to get “the nearest whole number to zero”.

If you want to round up, append the .toFixed() function, as in:

[[ ((Now.valueOf() - Date.parse(‘12/31/2021’).valueOf()) / 1000 / 60 / 60 / 24).toFixed() ]]

• Optionally, you can include the number of decimal digits as an argument, as in, [[ myValue.toFixed(2) ]] --if omitted, zero is assumed.

Brilliant! Thanks so much.