Hi all, i have this question: is it possible to split a float in two int using the comma as divisor?
Thank you all.
If you want to convert a float to two ints, subtract the truncated int value from the float, then multiply the remaining fraction by 10000.
Do you mean that you want 12,675 to become 12 and 675?
float val = 12,675;
int whole = val;
int remainder = (val - whole) * 1000;
Mmm... sorry, is not very clear how for me.
I was searching something like this: split() / Reference / Processing.org
For exampe I've 1050,62. I split with the "," and I obtain 1050 and 62.
Are you talking about a mathematical way to do it instead than a command?
I see... yes it have to work, so simple and smart!
But, just for know, there is also a processing like function to do it (for example to make the same with a text).
But, just for know, there is also a processing like function to do it (for example to make the same with a text).
Splitting a string is completely different from dividing and truncating a float value.
If can be done, using strtok().
secondsky:
For exampe I've 1050,62. I split with the "," and I obtain 1050 and 62.
Is that a string "1050,62" or a floating-pint number?
For a floating-point number:
float myFloat = 1050.62;
int firstPart = myFloat;
myFloat -= firstpart;
int secondPart = myFloat * 100000;
while (secondPart > 0 && (secondPart % 10) == 0)
secondPart /= 10;
This should leave you with 1050 in firstPart and 62 in secondPart.
int secondPart = myFloat * 100000;
BZZZZZT
AWOL:
int secondPart = myFloat * 100000;
BZZZZZT
OK, OK. Make that:
int secondPart = myFloat * 1000;
or
long int secondPart = myFloat * 100000;
And if the value might be less than 0 be sure to use the absolute value.
proposed code might not work with leading zero's in the second part: check - 1050.0062 would result in 1050 and 62 or ...
So you should split the float in three longs if you want to reconstruct it..
- W : the whole part - before the .
- N : a numerator - for the fraction part
- D: a denominator - for the fraction part
F = W + N/D
alternative for D one could store E the exponent of power of 10
F = W + N * 10^E