# Scaled\_float rounding oddly

**URL:** <https://discuss.elastic.co/t/scaled-float-rounding-oddly/356025>\
**Category:** Elasticsearch\
**Created:** [March 23, 2024, 3:50am UTC](https://discuss.elastic.co/t/scaled-float-rounding-oddly/356025 "2024-03-23T03:50:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garethhumphriesgkc](https://avatars.discourse-cdn.com/v4/letter/g/eb8c5e/32.png) [@garethhumphriesgkc](https://discuss.elastic.co/u/garethhumphriesgkc)\
**Post date:** [March 23, 2024, 3:50am UTC](https://discuss.elastic.co/t/scaled-float-rounding-oddly/356025/1 "2024-03-23T03:50:06Z")

</div>

I thought I understood scaled float, and up until today they have worked as expected, but I indexed some new data and scaled\_float fields got rounded in a way I can't explain.

Consider the below mapping - taken direct from the index in question:

```auto
"daily": {
        "properties": {
          "cost": {
            "type": "scaled_float",
            "ignore_malformed": false,
            "scaling_factor": 6,
            "coerce": true
          },
          "cost_per_kwh": {
            "type": "scaled_float",
            "ignore_malformed": false,
            "scaling_factor": 4,
            "coerce": true
          },
          "kwh": {
            "type": "scaled_float",
            "ignore_malformed": false,
            "scaling_factor": 3,
            "coerce": true
          }
        }
      }

```

Now consider the below document found in it:

```auto
{
  "_source": {
    "daily": {
      "kwh": 10.197,
      "cost": 4.45,
      "cost_per_kwh": 0.436403
    },
  },
  "fields": {
    "daily.kwh": [
      10.333333333333334
    ],
    "daily.cost": [
      4.5
    ],
    "daily.cost_per_kwh": [
      0.5
    ]
  }

```

You can see the correct values in the `_source` - this is what I expect to saved as a scaled\_float, yet all three numbers are being rounded oddly.

Take for example cost - it has a scaling factor of 6 so I'd expect 6 decimal points of precision to be saved - yet the source value of `4.45` has been stored as `4.5`.  
Then also consider `kwh`, which not only has a similar rounding error, but has 15 decimal points of precision despite having a scaling factor of 3!

I find this most perplexing- how can it be?

---

<div class="post-metadata">

**Author:** ![garethhumphriesgkc](https://avatars.discourse-cdn.com/v4/letter/g/eb8c5e/32.png) [@garethhumphriesgkc](https://discuss.elastic.co/u/garethhumphriesgkc)\
**Post date:** [March 24, 2024, 10:03pm UTC](https://discuss.elastic.co/t/scaled-float-rounding-oddly/356025/2 "2024-03-24T22:03:12Z")

</div>

I found my problem. The `scaled_float` parameter isn't the number of figures after the decimal point, it's just the number to multiply the input value by - so if you want 6 digits of precision you need to use `10^6` or `1000000` in this field.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 21, 2024, 10:03pm UTC](https://discuss.elastic.co/t/scaled-float-rounding-oddly/356025/3 "2024-04-21T22:03:57Z")

</div>

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