1

I am requesting values over API. Something like this: array = [{'value': 4.3}, ...]. These numbers are shown correctly also in the debug mode and in console.log output or in API response.

I am using then this code in the front end:

                    <tr>
                        <th scope="col"></th>
                        <td  scope="col" v-for="(value, key) in array">
                            <span> {{ value }}</span>
                        </td>
                    </tr>

I see "4.3" after rendering.

But if I change it like this:

                    <tr>
                        <th scope="col"></th>
                        <td  scope="col" v-for="(value, key) in aindex_array">
                            <span> {{ 6 - value }}</span>
                        </td>
                    </tr>

I see "1.7000000000000002" after rendering.

Same if I use "6.0".

Strange thing, it happens not with all number but only some of them. What is this and how can I avoid it?

In case it is relevant: On the backend side of the API are Gunicorn and Flask.

Kasper747
  • 31
  • 4
  • Thanks, I know how to round numbers in js. I would like to understand at which point I this error is being created and how I can handle it in a different way. – Kasper747 Aug 28 '18 at 11:11
  • 2
    It's not a vue thing, if you do the same thing in browser's console, the result will still be the same `1.7000000000000002`. – dziraf Aug 28 '18 at 11:17
  • Just tested it. You are absolutely correct. Did not thought about testing it this way. – Kasper747 Aug 28 '18 at 12:08
  • How can I close the question or should I delete it? – Kasper747 Aug 28 '18 at 12:10
  • This likely has to do with the machine (binary) representation of the floating point number. Floating point representations in many languages are often subject to accuracy issues like this. Here is an example of another post discussing a similar issue https://stackoverflow.com/questions/11695618/dealing-with-float-precision-in-javascript . With regard to deleting the question, it's probably best to leave the question as it is a reasonable question and others may encounter it as well. I can vote to close it as a duplicate though. – RonC Aug 28 '18 at 12:46
  • Closed as a duplicate. Thanks! – Kasper747 Aug 28 '18 at 14:32

0 Answers0