• AVR Freaks

Rounding floating point numbers after 2 decimal places

Author
litunbls
Junior Member
  • Total Posts : 107
  • Reward points : 0
  • Joined: 2016/12/28 10:29:43
  • Location: 0
  • Status: offline
2017/08/27 00:00:03 (permalink)
0

Rounding floating point numbers after 2 decimal places

Dear all,
           I use MPLAB X IDE with XC16. 
          I want to round a floating point numbers after 2 decimal places.
Ex--
float x=3.1459;
float y;
 
I want ro round 'x' up tp 2 decimal places and store it in 'y' so that y=3.15.
#1

13 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 00:05:22 (permalink)
    4 (1)
    y = round(x*100)/100;
     
    #2
    litunbls
    Junior Member
    • Total Posts : 107
    • Reward points : 0
    • Joined: 2016/12/28 10:29:43
    • Location: 0
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 00:13:08 (permalink)
    0
    qhb
    y = round(x*100)/100;
     


    Thanks....but which header file i need for round finction.
     
    Can any one reply is there any other method to do so.????????????
    post edited by litunbls - 2017/08/27 00:43:19
    #3
    i1ctg
    Super Member
    • Total Posts : 206
    • Reward points : 0
    • Joined: 2012/10/18 08:43:35
    • Location: OMEGNA-VB- ITALY
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:05:24 (permalink)
    4 (1)
    You have to go back to school.
     
    int z;
    z = (x+0.005)*100:
    y=z/100;
    post edited by i1ctg - 2017/08/27 01:11:52
    #4
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:05:32 (permalink)
    3 (1)
    You're right, XC16 doesn't supply that function.
    You could make your own like this:
    float round (float x)
    {
        return floor(x + 0.5f);
    }

    #5
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:09:07 (permalink)
    4 (1)
    i1ctg
    y=z/100;

    Will always return an integer value. This might work better.
    y=z/100.0;
     
    #6
    litunbls
    Junior Member
    • Total Posts : 107
    • Reward points : 0
    • Joined: 2016/12/28 10:29:43
    • Location: 0
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:11:20 (permalink)
    0
    qhb
    You're right, XC16 doesn't supply that function.
    You could make your own like this:
    float round (float x)
    {
        return floor(x + 0.5f);
    }



    But my idea is not to do........
     
              I want to round a floating point numbers after 2 decimal places.
    Ex--
    float x=3.1459;
    float y;
     
    I want ro round 'x' up tp 2 decimal places and store it in 'y' so that y=3.15.
    #7
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:14:46 (permalink)
    4 (1)
    So?
    Use the original code I gave you, plus the round() function I gave you.
    What's the problem?
     
    #8
    litunbls
    Junior Member
    • Total Posts : 107
    • Reward points : 0
    • Joined: 2016/12/28 10:29:43
    • Location: 0
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:21:11 (permalink)
    0
    Thank you ..a small problem.
     
    #include<iostream>
    #include<math.h>
    using namespace std;
    float round (float x);
    float g=3.14593;
    int main()
    {

    float s=round(g*100)/100;
    cout<<"op to 2decimal value= "<<s;

    return 0;
    }

    float round (float x)
     {
        return floor(x + 0.05f);
     }

     I am getting 3.14, but i wanted 3.15. :)
    #9
    litunbls
    Junior Member
    • Total Posts : 107
    • Reward points : 0
    • Joined: 2016/12/28 10:29:43
    • Location: 0
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:23:36 (permalink)
    0
    solved....
    i changed 
    float s=round(g*100.0)/100.0;
     
    Thank you very much.
    #10
    1and0
    Access is Denied
    • Total Posts : 11143
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:23:40 (permalink)
    3 (1)
    Try this:
    #include <math.h>
    float roundf(float x)
    {
        return (x >= 0.0) ? floorf(x + 0.5) : ceilf(x - 0.5);
    }
     
    y = roundf(x * 100) / 100;

    I hope you know that floating point is "inexact."
    #11
    qhb
    Superb Member
    • Total Posts : 9999
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:31:01 (permalink)
    3 (1)
    litunbls
    ...
    float round (float x)
     {
        return floor(x + 0.05f);
     }[/code]
     I am getting 3.14, but i wanted 3.15. :)

    You put 0.05 where I had 0.5
     
    #12
    litunbls
    Junior Member
    • Total Posts : 107
    • Reward points : 0
    • Joined: 2016/12/28 10:29:43
    • Location: 0
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 01:40:53 (permalink)
    0
    Hmm.....thanks.
    #13
    CinziaG
    die fucking humans
    • Total Posts : 3145
    • Reward points : 0
    • Joined: 2016/12/07 14:20:36
    • Location: Wien
    • Status: offline
    Re: Rounding floating point numbers after 2 decimal places 2017/08/27 02:32:46 (permalink)
    0
    I use this in my C++ class

    double CSkynet::round(double d,double n) {
        double d1;

        d1=(double)((int)((d+n/2)/n));
        d=d1*n;
        return d;
        }

     
    you get the point Smile
     

    in 2018 you signed for your annihilation. in 2019 it will come ;) I promise
    my most wonderful creations here
    https://www.youtube.com/c...dPFRvtwsbSTXp6Sk6azGOQ
    #14
    Jump to:
    © 2020 APG vNext Commercial Version 4.5