connectrpc / connect-es

The TypeScript implementation of Connect: Protobuf RPC that works.

Home Page:https://connectrpc.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Numbers with decimal points are decoded incorrectly

yehudamakarov opened this issue · comments

When sending a float like 5.99, the transport and client used with connect-es show a value in the response like 5.986273959362626

Hitting the http endpoint does not produce this result.

please note
The response is coming from a go api so this is definitely not at encoding

To Reproduce
Using a connect-es transport and client, request a payload with a float and once the response is ready the float will be inaccurate.

Environment (please complete the following information):

  • @connectrpc/connect-web version: latest
  • @connectrpc/connect-node version: latest
  • Frontend framework and version: react@18.2.0
  • Node.js version: 20
  • Is this intended or expected
  • What canonical way can I retain accuracy?

Hey Yehuda, this is indeed expected behavior. JavaScript Number is a double, and a single-precision value (protobuf float type) will format this way. It is exactly the same value as on your server, but it will render differently.

Here's the same issue for protobuf.js: protobufjs/protobuf.js#1512

I would recommend to use toFixed() or a Intl.NumberFormat for representational purposes, or simply switch to a protobuf double field type if appropriate.