Formatter .formatNumber is rounding 0.5 value incorrectly

I’m expecting values ending on 0.5 like 17.5 to be rounded up. But it’s rounded down.

The code:

The output:

  "input1": 17.5,
  "roundingTest1": 0.17,
  "input2": 17.51,
  "roundingTest2": 0.18

In case it matters: nodejs, wappler 6.3.3

The formatNumber indeed doesn’t round the number but uses toFixed internally. you can test the same result in the browser console with (17.5/100).toFixed(2) and (17.51/100).toFixed(2). This is the expected behavior.

Thanks!

What’s the best way to get 0.5 to round up using wappler’s formatters?

On the client-side you can use (input1 / 100).round(2). In NodeJS the round formatter doesn’t have the precision argument, but you can use input1.round() / 100 to get the same result.

If you wanted to just round a number with 2 decimal precision you use input1.round(2) on the client and on the server it becomes (input1 * 100).round() / 100 to get the same result.

On the server the formatters are not exactly the same as on the client, sometimes the name is different, they have different arguments or the order of the arguments differ. That makes it difficult to port the client formatters 1 on 1 to the server without braking backwards-compatibility.

5 Likes

Okay thank you for the elaboration @patrick :slight_smile:

Perhaps AppConnect 2 is a good time to streamline some things like the client side and server side formatters… good time to introduce breaking changes :wink: