Transaction Group Round Float Value

Hey all,
Having a hell of a time with this.
No matter what I do to an OPC Float tag in a transaction group I can not get it to round the decimal places to 2.
I have tried the following:
Formatted the tag on the tag itself with “#.#”, “#.00”, “0.00”
Created an expression tag in the transaction group using numberFormat({Tag},"#.00") (and variations of the above formats)
Created an expression tag in the transaction group using round({Tag},2) (and variations of the above formats)
Created an actual expression tag in the tag folder trying all of the above different formats.

The value will still get written to the database with more decimals than wanted.
Created a derived tag in the tag folder utilizing round, numberFormat, etc.

Nothing is working.
Any ideas?

Format it in the SQL INSERT statement. Post yours to show us how you’re doing it.
Most of us would probably store at supplied resolution and format it on retrieval from the database.

Tip: use `` backticks to format code. e.g., the expression language numberFormat({Tag}, "#.00"). For pasting blocks of code use the </> code formatting button.

I know how to paste code.
None of the above is code.

This is a Transaction Group so no SQL INSERT statement…

If the column type in the DB is REAL, they you might simply be getting the closest binary approximation of the two decimal places you want. Any level of rounding for decimals upon binary representations has to be performed at the point of display, not the point of storage.

The are float columns in MS Sql Server.
I can manually edit the records after the insert and get it down to 2 decimal places.

I think you need to use a column type of DECIMAL in SQL Server for that to work.

https://www.sqlshack.com/understanding-sql-decimal-data-type/

1 Like

We saw this today. SSMS showed a value of 0.0039, Ignition database Query Browser and EasyChart showed 0.004. In the EasyChart, created a ‘Calculated Pen’ that was the db pen in question X10 which now shows 0.039 (needed to make the original driving db pen enabled and hidden).

The most common cause of this discrepancy is sending 32-bit reals (aka Float) from a PLC to 64-bit reals (aka Double) in the database. Default rounding in most databases account for the different expectations.

But this can also be a simple rounding configuration error. Some people simply cannot wrap their mind around fact that many non-repeating decimals make repeating "binarials".

Why does Ignition Designer Database Query Browser (and Vision components like EasyChart) round the number rather than just show the same as it does in SSMS? Is there a way to set it to do what I want rather than me having to do my work around as described above?

IEEE-754 floating point has no exact representation of 0.0039 in 32 or 64 bits.

Every user interface is lying to you by rounding for presentation in some way. None is more 'correct' than others.

Ahhh, so your saying even though I am seeing 0.0039 in SSMS query - even that is not exactly what is in the db. Would you say my work around as described above is an ok way to deal with this or is there a more Ignition native way?

The solution is to use a data type in your database that stores actual values. Floating point is an approximation so you get funny results sometimes.

In TSQL you want to use the decimal datatype.

Sometimes you can cast as decimal in your select statement but that’s less reliable than storing the data as decimal datatype.