Rounding to n Decimal Places

Format numbers with precise decimal places using the format() function

Query

flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1>Augment Data] result{{Result Set}} repo --> 1 1 --> result
flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1>Augment Data] result{{Result Set}} repo --> 1 1 --> result
logscale
format("%.2f", field=value)

Introduction

To round a number to a specific number of decimal points, use format() rather than round(). By specifying a format string such as %.2f, it is possible to control the exact number of decimal places displayed in the output, with standard rounding rules applied automatically.

Example incoming data might look like this:

@timestampvalue
17420328000003.14159265
17420328600002.71828182
17420329200001.61803398
17420329800009.99999999
17420330400000.12345678
17420331000007.50000001
17420331600004.56789012

Step-by-Step

  1. Starting with the source repository events.

  2. flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1>Augment Data] result{{Result Set}} repo --> 1 1 --> result style 1 fill:#ff0000,stroke-width:4px,stroke:#000;
    flowchart LR; %%{init: {"flowchart": {"defaultRenderer": "elk"}} }%% repo{{Events}} 1>Augment Data] result{{Result Set}} repo --> 1 1 --> result style 1 fill:#ff0000,stroke-width:4px,stroke:#000;
    logscale
    format("%.2f", field=value)

    Rounds the field value to two decimal places.

  3. Event Result set.

Summary and Results

When using format(), rounding is performed using standard math rules. The format() rounds a number to a specific decimal accuracy.

Sample output from the incoming example data:

value_format
3.141592653.14
2.718281822.72
1.618033981.62
9.9999999910.00
0.123456780.12
7.500000017.50
4.567890124.57

Note

To round a number to the nearest integer, use round(). See Basic Rounding.