Splunk Search

How to convert the result to a 2-digit decimal and then convert it to a percentage

danliu
Loves-to-Learn Everything

Hi

How to convert the result to a 2-digit decimal and then convert it to a percentage.

 

index=p1991_m_tiltline_index_json_raw deviceid=8TILG02
|eval DID= "EMS "+substr(deviceid, 7)
|timechart limit=200 span=5m avg(drive.current.max) BY DID

danliu_0-1720672914130.png

 

Labels (2)
0 Karma

yuanliu
SplunkTrust
SplunkTrust

Mathematically your question has no solution.  You can use round function to convert a number to 2 decimals.  But how do you "convert" a number into percentage?  Unless values of drive.current.max represents a ratio, this makes no sense.  Percentage compared with what?

0 Karma

danliu
Loves-to-Learn Everything

Thank you very much for your reply. In fact, the returned result is indeed a percentage, and the returned data comes from Siemens PLC. '2398' is actually 23.98%, so I want to convert the result to 2 decimal places and add a percentage sign to the converted decimal.

0 Karma

PickleRick
SplunkTrust
SplunkTrust

OK, so it's not about "converting" as much as simply dividing by 100. When you divide by 100 you can use round() to strip the decimal part.

And if you want to display it as a string ending with a percent sign, use fieldformat so that the underlying value doesn't get rendered into a string (that would break reasonable sorting). You might even leave the original data as is and only divide within fieldformat.

Like this:

| fieldformat whatever=tostring(whatever/100)."%"

One caveat in your case might be that you're using timechart to prepare your data and your field name(s) will vary depending on your input data and I'm not 100% sure that fieldformat will work with foreach.

0 Karma
Get Updates on the Splunk Community!

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

[Puzzles] Solve, Learn, Repeat: Nested loops in Event Conversion

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

Your Guide to Splunk Digital Experience Monitoring

A flawless digital experience isn't just an advantage, it's key to customer loyalty and business success. But ...