Int of fixed decimal point

Dears,
Int need to display as fixed decimal point. For example, 1.00. But when I use math.round(), only 1 can be displayed. What can I do to fixed point of int?

@Leo What are you using the display the data? Grafana? InfluxDB dashboard? Or just in raw query results?

in raw query result, as screenshot fellows

Anyone could help me? :face_with_peeking_eye:

The _value column in your screenshot is a float, not an int, but the problem still remains. If a float value does not include a fraction, Flux truncates the display of that value at the decimal point. The only way to force a 0-padded decimal point for display purposes would be to convert the number to a string and append “.00”. If you’re just going to be viewing this as a table, this won’t matter, but this will not graph correctly as a string.

Replace your map call with this:

  |> map(fn: (r) => ({r with _value: "${roundTo(n: r._value, precision: 2.0)}.00"})))

Yes, _value are not int. Infact, the _value looks like as fellow screenshot:

  • what I want is that all data are fixed with two decimal, include 105.1 on the screenshot
  • I am use influxdb v2.1, this flux can not implement
|> map(fn: (r) => ({r with _value: "${roundTo(n: r._value, precision: 2.0)}.00"})))

I see. The data in your second screenshot is different than the data in your first. To zero-pad these float values, you still have to convert them to strings and add zeros. Here’s a custom function that would do it:

import "strings"

// zeroPad pads numeric values with a specific number of zeros beyond the decimal point.
//
// - n: Number of places to pad after the decimal. Be sure this number is greater than or
//      equal to the number of fraction digits included in the input value.
// - v: Numeric input value
zeroPad = (n=2, v) => {
  numberParts = strings.split(t: ".", v: string(v: v))
  whole = numberParts[0]
  hasFraction = length(arr: numberParts) > 1
  fraction = if hasFraction then
      numberParts[1] + strings.repeat(i: n - strings.strlen(v: numberParts[1]), v: "0")
    else
      strings.repeat(i: n, v: "0")

  return "${whole}.${fraction}"
}

// ... Your query up to the map() call
  |> map(fn: (r) => ({r with _value: zeroPad(v: r._value)}))

Thanks for your reply.
I wonder If that indicates that there is no build-in function like fixed, so I need custom in code

Correct. There is no built-in function that zero-pads float values because it generally isn’t necessary. This is a display-only feature so it there isn’t anything in the standard library for it.