Is there a way to round a real number to a specific number of decimal places in the S7/TIA Portal software? For example, if I have the number 22.519432 and I want to round it to 2 decimal places, the result should be 22.52. I am asking this question because the operator has mentioned that the data recorded in the CSV file displays 7 decimal places, but they would like it to match the HMI-displayed value, which is shown with 2 decimal places. I initially thought of using the ROUND instruction, but I believe it rounds to the nearest integer. Is there a different instruction available for this purpose, or should I consider using VAL_STRING instead? Thank you.
I no longer have access to TIA Portal, and it has been some time since I last used it. Is it possible to transform your REAL number by multiplying it by 100 to a DINT or INT and then dividing it by 100 back to another REAL? This method reminds me of how I would handle it in Allen Bradley PLC programming.
If you are encountering issues with discrepancies between the REAL representation in TIA and the floating point representation in Windows when writing to a CSV file, you may notice that values displayed as 22.52 in the PLC or HMI appear as 22.51999994 in Windows. Even rounding to 22.52 may not entirely resolve the issue. One potential solution is to utilize integers with an implied decimal point to address this discrepancy.
Instead of using the REAL data type in the PLC, it would be more advantageous to utilize VAL_STRG for logging strings. This will avoid issues with implied decimals and inaccuracies in data storage. JesperMP highlighted the limitations of the REAL data type, which does not have the capability to restrict the number of decimal places. By opting for VAL_STRG, you can ensure more precise and accurate storage of numerical values in the PLC.
There are various methods to customize the display of a CSV file in Excel. Instead of using the default format, you can create a template with specific settings, such as setting cells to display values to 2 decimal places. Another approach is to use a code for more accurate rounding up or down, following standard guidelines. Simply adjust the code for a different number of decimal places by changing the value in the code.
The challenge lies in how a 32-bit IEEE Floating Point number represents fractional decimals using structured binary. Unlike integers, there isn't always a precise decimal to binary conversion for fractional numbers. For instance, when the number 1.3 is closely approximated in 32-bit float format, it becomes 1.2999999523162841796875. The screenshot provided demonstrates an unexpected result from a simple addition operation, highlighting the intricacies at play. One solution could be storing values as integers with 2 implied decimal places by multiplying by 100, or implementing rounding when displaying the values. Understanding these nuances is crucial for accurate representation in computing.
While you're correct that the ROUND instruction may only round to the nearest integer, one common workaround for rounding to decimal places in S7/TIA is to multiply your number by 10 power n (where n is the number of decimal places you wish to round to), use the ROUND block, and then divide by 10 power n. So, for your example, you'd multiply 22.519432 by 100, round it, then divide it by 100. This method works well for both rounding and matching the HMI-displayed value. Remember though that for very large numbers, this method might cause overflow.
You're correct in stating that the ROUND instruction rounds real numbers to the nearest integer. One method you might consider is scaling up (multiplying) your number by 10^n (where n is the number of decimal places you want), then using the ROUND instruction, and finally scaling down (dividing) by the same 10^n. Thus, if you want 22.519432 to round to two decimal places, multiply by 100 to get 2251.9432, round to get 2252, then divide by 100 to get 22.52. This should maintain the decimal precision you want for your CSV files. Using VAL_STRING could also work, but it will convert your real number to a string format which you'd then have to convert back if you want to do further calculations with it.
You've touched a pretty common scenario. Though the ROUND instruction in S7 indeed rounds to the nearest integer, you still can use it to round to a specified decimal place by using a simple trick: multiply the original value by 10^n (n being the number of desired decimal places), then round, and then divide by 10^n. Therefore for rounding to two decimal places you could use: round_val:=ROUND(value*100)/100. However, this method can have some rounding issues because of how the Siemens PLC handles floating point numbers. To stay on the safe side, you might want to consider using the string approach that you mentioned or use an external library if your project permits.
You can definitely round to a specific number of decimal places in TIA Portal. The ROUND instruction works; however, you might need to adjust it because it can give you integer results if not used with the right scaling. A common method is to multiply your number by 100 (for two decimal places), round it, and then divide by 100 again. This would give you the correct two decimal place result. Alternatively, if you're dealing with CSV export, you could format the output string to display the desired number of decimal places, often a more straightforward solution for ensuring consistency. Give that a try!
You can definitely round numbers to a specific number of decimal places in TIA Portal using the proper math functions. Instead of the ROUND instruction, which indeed rounds to the nearest integer, you can use the combination of multiplying your number by a power of 10, applying the ROUND function, and then dividing it back. For two decimal places, you would do something like `ROUND(yourNumber * 100) / 100`. This way, you can ensure that the value matches what is shown on your HMI. If you're exporting to CSV, you might also want to format the output appropriately to only show two decimal places for consistency!
✅ Work Order Management
✅ Asset Tracking
✅ Preventive Maintenance
✅ Inspection Report
We have received your information. We will share Schedule Demo details on your Mail Id.
Answer: Answer: In Siemens S7/TIA Portal software, you can achieve rounding of real numbers to specific decimal places by using the VAL_STRING function instead of the ROUND instruction, as the ROUND instruction rounds to the nearest integer.
Answer: Answer: Rounding real numbers to specific decimal places is often necessary to match the displayed values on HMIs or to adjust the precision of data recorded in files, such as CSV files.
Answer: Answer: To ensure consistency between the data displayed on an HMI with 2 decimal places and the data recorded in a CSV file with 7 decimal places, you can implement rounding functions to format the values accordingly.
Join hundreds of satisfied customers who have transformed their maintenance processes.
Sign up today and start optimizing your workflow.