Decimal part from a Float


+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Decimal part from a Float59 days old

  1. #1
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    340
    Thumbs Up
    Received: 10
    Given: 1
    Total Downloaded
    7.87 GB

    0 Not allowed!

    Default 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
    Thank you in advance,
    Teo

  2. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  3. #2
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Posts
    2,560
    Thumbs Up
    Received: 76
    Given: 33
    Total Downloaded
    6.56 GB

    0 Not allowed!

    Default 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.
    Regards

    Joseph

  4. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  5. #3
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  7. #4
    Prolific Poster joesaliba's Avatar
    Join Date
    Sep 2004
    Posts
    2,560
    Thumbs Up
    Received: 76
    Given: 33
    Total Downloaded
    6.56 GB

    0 Not allowed!

    Default 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
    Last edited by joesaliba; 29th March 2020 at 12:27.
    Regards

    Joseph

  8. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  9. #5
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,884
    Thumbs Up
    Received: 91
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default 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.
    Last edited by John Drew; 29th March 2020 at 13:02. Reason: Saw Bob and Joseph's replies

  10. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  11. #6
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    340
    Thumbs Up
    Received: 10
    Given: 1
    Total Downloaded
    7.87 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  13. #7
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    340
    Thumbs Up
    Received: 10
    Given: 1
    Total Downloaded
    7.87 GB

    0 Not allowed!

    Default 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
    Thanks in advance
    Teo

  14. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  15. #8
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,884
    Thumbs Up
    Received: 91
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  17. #9
    Member keytapper's Avatar
    Join Date
    Jun 2019
    Posts
    192
    Thumbs Up
    Received: 8
    Given: 5
    Total Downloaded
    331.75 MB

    0 Not allowed!

    Default Re: Decimal part from a Float

    Quote Originally Posted by joesaliba View Post
    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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  19. #10
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,884
    Thumbs Up
    Received: 91
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  21. #11
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    340
    Thumbs Up
    Received: 10
    Given: 1
    Total Downloaded
    7.87 GB

    0 Not allowed!

    Default 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
    Thank you in advance,
    Teo

  22. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  23. #12
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    340
    Thumbs Up
    Received: 10
    Given: 1
    Total Downloaded
    7.87 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  25. #13
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  27. #14
    Member teo's Avatar
    Join Date
    Sep 2010
    Posts
    340
    Thumbs Up
    Received: 10
    Given: 1
    Total Downloaded
    7.87 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  29. #15
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Display no decimal point on a float.
    By geoff_c in forum Proton Plus Compiler v3
    Replies: 7
    Last Post: 8th February 2017, 20:42
  2. Decimal Digit extract
    By John Drew in forum Proton Plus Compiler v3
    Replies: 0
    Last Post: 31st January 2017, 01:38
  3. Binary or decimal?
    By joesaliba in forum Proton Plus Compiler v3
    Replies: 12
    Last Post: 25th August 2008, 19:59
  4. [SOLVED !] 7segment Decimal Point Value
    By willim in forum Proton Plus Compiler v3
    Replies: 11
    Last Post: 26th April 2007, 19:49
  5. [SOLVED !] bcd to decimal
    By ADLIN SYSTEMS in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 31st March 2007, 14:24

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts