Decimal part from a Float

# Thread: Decimal part from a Float – 59 days old

1. 0

## Decimal part from a Float

Hello everybody,
How can I extract the decimal part from a float?
a = 123.456
I have to get two variables:
b = 123
c = 456
Teo

2. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

3. 0

## Re: Decimal part from a Float

Teo,

Can you multiply the number by 1000, and then use the Dig command? I.e.: -

Code:
```Dim myFloat As Float = 123.456
Dim tDword  As Dword
Dim aResult As Word = 0
Dim bResult As Word = 0
Dim Temp[6] As Word
Dim I As Byte

tDword = myFloat * 1000

Main:

For I = 0 To 5
Temp[I] = Dig tDword , I
Next

Temp[1] = Temp[1] * 10
Temp[2] = Temp[2] * 100
Temp[4] = Temp[4] * 10
Temp[5] = Temp[5] * 100

aResult = Temp[0] + Temp[1] + Temp[2]
aResult = Temp[3] + Temp[4] + Temp[5]```
This is off my mind and not tested but you get the idea.

4. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

5. 0

## Re: Decimal part from a Float

If you simply want to extract the integer and fraction parts of a floating point number....

Dim floatnum As Float = 123.456
Dim fractnum As Float
Dim intnum As Byte

intnum = floatnum 'integer
fractnum = floatnum - intnum 'fraction

Bob

6. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

7. 0

## Re: Decimal part from a Float

Bob,

I should not post when I just wake up. Your solution is very simple and neat!

But still I think that after: -

Code:
`fractnum = floatnum - intnum 'fraction`
should be multiplied by 1000 to get rid of the decimal point, i.e.: - as it is answer will be 0.456

Code:
`fractnum = fractnum * 1000`
fractnum will come 456

8. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

9. 0

## Re: Decimal part from a Float

Here was my similar solution:
Dim Big As Word
Dim Frac As Word
Dim Decimal As Float
Dim Temp As Dword

Main:
Decimal = 1234.567
Temp = Decimal * 1000
Big = Temp / 1000
Frac = Temp // 1000
Print At 1,1,Dec Big," "
Print At 2,1,Dec Frac," "
End
John

Good one Bob, that's the simplest. There's many ways of doing this. Bob's answer does give a float, you might prefer an integer result.

10. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

11. 0

## Re: Decimal part from a Float

I don't know how many decimals there are.
There may be one or more.
Teo

12. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

13. 0

## Re: Decimal part from a Float

I have to transform "smart" float into integer.
if:
a = 123.49 then a = 123
if:
a = 123.5 then a = 124
Teo

14. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

15. 0

## Re: Decimal part from a Float

Teo,
Look up fRound in the manual.
For some more info on maths especially the limitations of floats and using integers in their place have a look at this article I wrote a few years back
Home/Wiki/Tips and Tricks/Math/Floating point issues and alternatives

It is a little dated as since then Les has introduced fRound and simplified things but the essentials are still correct.
There's a lot to be said for going to PIC24 and using 64 bit floats.
John

16. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

17. 0

## Re: Decimal part from a Float

Originally Posted by joesaliba
use the Dig command
Great tip, I wasn't aware of.

18. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

19. 0

## Re: Decimal part from a Float

Teo, it doesn't matter how many numbers after the decimal point. Just multiply and divide by the maximum you'll need.
E.g if the maximum is 3 then use 1000, if 4 then use 10000.
You'll just have to watch out for any overflows in the multiply line.
John

20. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

21. 0

## Re: Decimal part from a Float

I have the following code:
Main:
Clear Temp_rec
MyIndex = 0
Initial = 0
Do
Initial = HRSI
If Initial = 13 Then Break
Temp_rec [MyIndex] = Initial
MyIndex Inc.
If MyIndex> = 4 Then
MyIndex = 0
endif
Loop

Temp [0] = Temp_rec [0] - 48
Temp [1] = Temp_rec [1] - 48
Temp [2] = Temp_rec [2] - 48
Temp_Dec = (Temp [0] * 100) + (Temp [1] * 10) + Temp [2]
Product = k * Temp_Dec
Rez_PT1000 = Product + 1000
Rez_PT1000_round = fRound (Rez_PT1000)
GoTo Main
this code affects "Temp_rec [2]"
if I replace Rez_PT1000_round = fRound (Rez_PT1000) with
Rez_PT1000_round = fRound (121,456)
everything is OK
"fround" cannot be applied to a variable in the code above
that's why I tried another variant of transforming a float into an integer
Teo

22. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

23. 0

## Re: Decimal part from a Float

Basically I have to receive four ASCII characters. The first three represent the temperature of the PT1000 sensor (0-250C) and the last "CR"
Then to obtain in a variable the value of the resistance of the PT1000 sensor.
For Temp_rec = 0 we have Rez_PT1000_round = 1000
............
For Temp_rec = 100 we have Rez_PT1000_round = 1381
Teo

24. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

25. 0

## Re: Decimal part from a Float

Teo,

The following line will do the rounding up for you...

intnum = (floatnum +0.5)

If floatnum = 123.456 then intnum becomes 123. Should floatnum = 123.5 then intnum would become 124.

I assume that the fractional part of the number is not required.

Bob

26. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

27. 0

## Re: Decimal part from a Float

Hi Bob,
Same problem
I'm in a big mess
Teo

28. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

29. 0

## Re: Decimal part from a Float

Out of interest, what's the value of K Teo?

30. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues