I have a float value that I would like to convert what is on each side of the decimal point to an int.
Example
C = 234.16
coverts to two int values.
A = 234
B = 16
Can someone help me with this?
I have a float value that I would like to convert what is on each side of the decimal point to an int.
Example
C = 234.16
coverts to two int values.
A = 234
B = 16
Can someone help me with this?
Can the float value be negative? If so, there is a question of sign.
You could try the following, which works for only for some range of values.
float x = 234.16; //whatever
long int y=x;
float remainder = x - (float)y;
Serial.println(y);
Serial.println(remainder);
You need to multiply the remainder by some power of ten before converting to an integer, which depends on how many digits to the right of the decimal are of interest.
Be aware that a float value can have a TOTAL of 6 to 7 valid decimal digits, but no more.
@jfarnham
You have a problem with this unless you have a fixed number of decimal digits. If you split 234.16 you get 234 and 16, but what about 234.5? That should not be 234 and 5, because the two digit representation is actually 234.50
Do you have a system that handles this properly?
float X;
int Y;
In the loop or in your function
Y = int(round(X));
I have a float value that I would like to convert what is on each side of the decimal point to an int.
What are you going to do with the result ?
jfarnham:
I have a float value that I would like to convert what is on each side of the decimal point to an int.Can someone help me with this?
float C = 234.16;
int A = C;
int B = ((C - A) * 100); // two digits after the decimal point
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println(C);
Serial.println(A);
Serial.println(B);
}
void loop() {}
Output:
234.16
234
16
WARNING: If there is a leading 0 in the decimal part, that will not be shown when printing 'B'. To print a single-digit integer as two digits you have to supply the leading zero:
if (B < 10)
Serial.print('0');
Serial.println(B);
couldn't you do
int B = (A * 100) % 100;
gcjr:
couldn't you doint B = (A * 100) % 100;
Nope. 'A' is an integer and already lost its decimal places. You will always get 0.
This should work for values of C below 327.68 (higher values won't fit in an 'int' when multiplied by 100).
int B = int(C * 100) % 100;
I don't think the modulo operator (%) works on floats, which is why you have to cast to 'int' first.
OOPS... I should put a warning that ".05" will come out as the integer '5'
more specifically
output
c 123.450, b 44
c 123.450, b 44
float c = 123.45;
int b = int(100 * c) % 100;
printf (" c %6.3f, b %6d\n", c, b);
int a = c;
b = (c - a) * 100;
printf (" c %6.3f, b %6d\n", c, b);
gcjr:
float c = 123.45;
int b = int(100 * c) % 100;
printf (" c %6.3f, b %6d\n", c, b);
int a = c;
b = (c - a) * 100;
printf (" c %6.3f, b %6d\n", c, b);
I get two compiler warnings and no output at all when I run this on my UNO:
void setup()
{
Serial.begin(115200);
while (!Serial);
float c = 123.45;
int b = int(100 * c) % 100;
printf (" c %6.3f, b %6d\n", c, b);
int a = c;
b = (c - a) * 100;
printf (" c %6.3f, b %6d\n", c, b);
}
void loop() {}
Warnings:
/Users/john/Documents/Arduino/sketch_jul17a/sketch_jul17a.ino: In function 'void setup()':
/Users/john/Documents/Arduino/sketch_jul17a/sketch_jul17a.ino:9:37: warning: format '%f' expects argument of type 'double', but argument 2 has type 'float' [-Wformat=]
printf (" c %6.3f, b %6d\n", c, b);
^
/Users/john/Documents/Arduino/sketch_jul17a/sketch_jul17a.ino:13:37: warning: format '%f' expects argument of type 'double', but argument 2 has type 'float' [-Wformat=]
printf (" c %6.3f, b %6d\n", c, b);
^
Sketch uses 2962 bytes (9%) of program storage space. Maximum is 32256 bytes.
Global variables use 208 bytes (10%) of dynamic memory, leaving 1840 bytes for local variables. Maximum is 2048 bytes.
When I modify it to 'work' on an UNO, I get slightly different output:
void setup()
{
char buffer[80];
Serial.begin(115200);
while (!Serial);
double c = 123.45;
int b = int(100 * c) % 100;
sprintf (buffer, " c %6.3f, b %6d\n", c, b);
Serial.print(buffer);
int a = c;
b = (c - a) * 100;
sprintf (buffer, " c %6.3f, b %6d\n", c, b);
Serial.print(buffer);
}
void loop() {}
Output:
c ?, b 45
c ?, b 44
The '?' comes from the fact that sprintf() in the AVR runtime library doesn't support floating-point format. I'm not sure why 'b' comes out as 45 in one case and 44 in the other. I suspect "(123.45 - 123) * 100" results in 44.9... which gets truncated to 44. Indeed, adding this shows 44.999694:
int a = c;
float d = (c - a) * 100;
b = d;
Serial.println(d, 6);
i ran the code using gcc on my laptop
johnwasser:
I'm not sure why 'b' comes out as 45 in one case and 44 in the other. I suspect "(123.45 - 123) * 100" results in 44.9... which gets truncated to 44.
may need to round by adding 0.005
float num = 123.45;
long numInt = num * 100.0; //now num == 12345
int fractionalPart = numInt % 100;
long integerPart = numInt / 100;
OP didn't say how or if rounding should be applied. I think this will produce 123 and 45 from 123.459999999999999. Whether that is correct or not depends on your definition and application. As mentioned above, you can round if you like.
Wow thanks for all the tips. My project is a duct tape dispenser that uses a rotary encoder that rides along the perimeter of the roll of tape. When tape is pulled the encoder turns driving an OLED display. Whole number inches will be displayed as integer numbers while in between will be illustrated on a graduated scale.
That is all very interesting, but does not explain why you need to do this.
jfarnham:
Wow thanks for all the tips. My project is a duct tape dispenser that uses a rotary encoder that rides along the perimeter of the roll of tape. When tape is pulled the encoder turns driving an OLED display. Whole number inches will be displayed as integer numbers while in between will be illustrated on a graduated scale.
If you just want the fractional part of a decimal number, just try something like:
float x = 123.456; // or whatever floating-point number
float y = x - (int)x; // this gets you the fractional part of x
// for example, if x is 123.456, then y will be 0.456
For numbers larger than 32767, you will need to use long instead of int to get this to work. But I think that, for your project, int will be plenty.
(int)x gets you the integer part of x. For example, if x is 25.8, then (int)x will be 25. If, instead, x is -25.8, then (int)x will be -25.
If, from any number, you subtract the integer part, what is left is the fractional part. That is all there is to it.