Splunk Search

Rounding Currency to Two Digits After Decimal

msettipane
Splunk Employee
Splunk Employee

I am attempting to round my currency figures to two decimal places whether or not the amount ends with zeros (e.g., $4.00, $0.10); however, it is returning $4 and $0.1.

I am using the following search command: eval cost = "$ " . tostring(round(count*.02, 2), "commas").

Tags (1)
1 Solution

Marklar
Splunk Employee
Splunk Employee

Doing this correctly requires some knowledge about significant figures and precision.

In multiplying by the number .02 (which has 1 significant figure), you limit your result to 1 significant figure. The precision is then set from that result.

What you seem to be looking for is exact arithmetic with a precision of 2. The exact() function forces a number to have maximum sigfigs and precision, while the round() function can force your precision. Assuming that count is a non-negative integer, this solution should work for you:

eval cost = "$ " . tostring(round(count*exact(.02), 2), "commas")

View solution in original post

Marklar
Splunk Employee
Splunk Employee

Doing this correctly requires some knowledge about significant figures and precision.

In multiplying by the number .02 (which has 1 significant figure), you limit your result to 1 significant figure. The precision is then set from that result.

What you seem to be looking for is exact arithmetic with a precision of 2. The exact() function forces a number to have maximum sigfigs and precision, while the round() function can force your precision. Assuming that count is a non-negative integer, this solution should work for you:

eval cost = "$ " . tostring(round(count*exact(.02), 2), "commas")

marco_sulla
Path Finder

IMHO this is completely unexpected and unwanted in the majority of cases. It is supposed this is not a framework for physical calcula...

0 Karma
Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Dynamic formatting from XML events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Enter the Agentic Era with Splunk AI Assistant for SPL 1.4

  🚀 Your data just got a serious AI upgrade — are you ready? Say hello to the Agentic Era with the ...

Stronger Security with Federated Search for S3, GCP SQL & Australian Threat ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...