Thread: Seprating floating value

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    11

    Seprating floating value

    Dear all,

    I have variable named float latitude=13.08. i wanted to separate 08 save it has integer . let me know how to do it in c

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    What have you tried?

    The focus of folks here is more on equipping you to solve problems, rather than just giving you solutions without any effort on your part. So you have to demonstrate some effort, rather than just demanding a solution.

    You would also need to decide what should happen if the value does not have only two decimal places (such as 13.081). There is also a wrinkle in that a floating point variable cannot exactly represent all multiples of 0.01 (it only represents an approximation) and your solution would need to cope with that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    11
    Quote Originally Posted by grumpy View Post
    What have you tried?

    The focus of folks here is more on equipping you to solve problems, rather than just giving you solutions without any effort on your part. So you have to demonstrate some effort, rather than just demanding a solution.

    You would also need to decide what should happen if the value does not have only two decimal places (such as 13.081). There is also a wrinkle in that a floating point variable cannot exactly represent all multiples of 0.01 (it only represents an approximation) and your solution would need to cope with that.

    Code:
    #define ID   1
    #include<math.h>
    
    
    Modbus slave(ID, 0, 0);
    boolean led;
    int8_t state = 0;
    unsigned long tempus;
    // data array for modbus network sharing
    uint16_t au16data[9];
    
    double  latitude=13.08;
    static int Sensor_Value;
    float Yvoltage;
    static float ARDUINO_ANALOG_SCALING = 0.00488758;
    static float Ydegree;
    
    
    void setup() {
      // define i/o
      pinMode(2, INPUT);
      pinMode(3, INPUT);
      pinMode(4, INPUT);
      pinMode(5, INPUT);
      pinMode(6, OUTPUT);
      pinMode(7, OUTPUT);
      pinMode(8, OUTPUT);
      pinMode(9, OUTPUT);
      pinMode(10, OUTPUT);
      pinMode(11, OUTPUT);
      pinMode(13, OUTPUT);
    
      digitalWrite(6, LOW );
      digitalWrite(7, LOW );
      digitalWrite(8, LOW );
      digitalWrite(9, LOW );
      digitalWrite(13, HIGH );
      
    
      // start communication
      slave.begin( 9600 );
    
      tempus = millis() + 100;
      digitalWrite(13, HIGH );
    }
    
    
    void loop() {
      // poll messages
      // blink led pin on each valid message
      state = slave.poll( au16data, 9 );
      if (state > 4) {
        tempus = millis() + 50;
        digitalWrite(13, HIGH);
      }
      if (millis() > tempus) digitalWrite(13, LOW );
    
      
      // read analog inputs
      
       Sensor_Value=analogRead(A3); 
      Yvoltage = Sensor_Value * ARDUINO_ANALOG_SCALING;
       Ydegree=(30*Yvoltage)-75;
    /*  mySerial.println(Ydegree);
      delay(1000);*/
      au16data[0] = Sensor_Value;
      int lat_int=(int)latitude;
      float lat_float=(latitude-lat_int)*1000;
      //Serial.print((lat_float));
      int value=(int)lat_int;
     au16data[1]=lowByte(lat_int);
     au16data[1]=highByte(value);
    //   Serial.println( au16data[1]);
      
    
      au16data[6] = slave.getInCnt();
      au16data[7] = slave.getOutCnt();
      au16data[8] = slave.getErrCnt();
    }
    Here i am reading value 0 in floating point , WHich work fine over python

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from multiplying by 1000 instead of 100 at line 68, and the need to assign value to be (int)lat_float at line 70, you've largely done it.

    Bear in mind that converting a float to an int rounds toward zero, so (int)26.9 will be 26, not 27.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there a way to validate floating value?
    By kekewong in forum C Programming
    Replies: 5
    Last Post: 10-10-2009, 03:21 PM
  2. Floating points
    By yann in forum C Programming
    Replies: 13
    Last Post: 09-13-2009, 01:16 PM
  3. Floating toolbar
    By eth0 in forum Windows Programming
    Replies: 2
    Last Post: 02-22-2006, 11:20 AM
  4. Floating-Point (BCD)
    By zx-1 in forum C Programming
    Replies: 1
    Last Post: 10-15-2004, 01:11 AM
  5. Floating variables
    By Granger9 in forum C Programming
    Replies: 3
    Last Post: 11-07-2002, 08:13 PM

Tags for this Thread