How do I find the whole number in a float?

WireGuy1950

Member
Join Date
Jun 2005
Location
Fairport Harbor, Ohio
Posts
155
I am trying to determine the fractional protion of a float number to do a further math operation and I am stumped on how to determine the "whole number" portion of the Float number, e.g. Float= 4.714286. I want to extract the "4" and subtract it from the float then multiply the remainder by 7.

The PLC is AB PLC 5/20.

Any suggestions?
 
The simplest way is to subtract .5 from the float and then use the MOV instruction to move the float to an integer. This works well for numbers that are between -32767.5 32767.5.

For larger numbers you need a truncate function. The PLC/5 and SLC500 do not have a simple truncate instruction. I have written one and you can download it at http://forums.mrplc.com/index.php?download=761

The program contains two different truncation examples and one rounding example. The rounding example is to round a float that is larger than 32767. Either one of the truncation examples should work for you. It was made for a SLC500 but will work on the PLC/5 also. If you only have RSLogix5 and not RSLogix500 let me know and I'll post a conversion.
 
Last edited:
Thank you both for the quick replies.

I wound up using the simple compare & subtract method, but I am keeping the other method for future reference.

I am starting another thread showing the results of this assistance in calculating the Day of the Week.

Thanks again.
 
The most efficient way with numbers in the integer range is to use

CPT N7:0 F8:0-0.5

No branching and no comparison involved. Integer to float comparisons require quite a bit of processor work and the CPT skips the necessity of making an intermediate memory storage.
 
What I found strange on a machine someone else programmed for us, that instead of just using the MOV command when they wanted to round off a Float to an Integer, they instead used MUL by 1.

It's just really odd to see a bunch of multiplying by 1 (they use it a LOT, everything that's displayed on the HMI is rounded).
 

Similar Topics

Hi, One of my customers has an old fabric tensile testing machine. The IC # AD7501KN of its controller has malfunctioned. This IC is related to...
Replies
1
Views
62
Hello everyone, I am a student and would like to take the next step and learn FactoryTalk (Batch preferably) and how to create HMIs etc. Would...
Replies
4
Views
453
Hi, Have a look at this picture... How can I find out the memory address of this tag? It was created by adding it to DB "Data_block_1", but I...
Replies
6
Views
891
I've got TIA portal v18, KTP1200 HMI. I want to add a slider bar (if that's the correct name for it) to an HMI screen. Baslcally, like a...
Replies
8
Views
977
I've got a SIMATIC HMI KTP1200 and hooking it up to PLC (s7-1200) for first time. On the HMI setup screen, it's asking me to enter: IP address...
Replies
5
Views
1,240
Back
Top Bottom