I want to ask about better solution for rounding numbers, which I got from

`moment.duration().asDays()`

the function gets the length of the duration in days, like:

`9.340782789351852`

In this case, is it better to use `Math.round()`

or `Math.floor()`

? Maybe you suggest to use another function.

Thank you in advance.

As you are no doubt aware, the `Math.round`

method returns the value of a number rounded to the nearest integer, whereas `Math.floor`

returns the largest integer less than or equal to a given number.

In your example, both `Math.round(9.340782789351852)`

and `Math.floor(9.340782789351852)`

would produce 9.

However, if the value is `9.540782789351852`

, `Math.floor(9.540782789351852)`

will return 9, whereas `Math.round(9.540782789351852)`

will return 10.

So the question becomes: what are you attempting to store?

It might be a better approach to store everything as seconds, then convert when you need it.

As an aside, you might also want to look into `.toFixed()`

which formats a number using fixed-point notation.

```
9.340782789351852.toFixed(2)
=> "9.34"
9.549782789351852.toFixed(2)
=> "9.55"
```

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.