Inputting a whole number with a fraction

I'm trying to think of the best way to input a number such as 20 3/4 in the serial port, and use it later in the program in a math function, etc. However, I'm not really sure how to convert the fraction?

I would like for it to be entered at the same time, is that possible?

Any suggestions would be appreciated greatly

First, you will need to delimit the fraction from the preceding number. I suggest a space. This will keep you from reading 21 11/16 as 211 1/16.

As for converting the fraction.. same way you do it manually. Assuming you want to convert it to a decimal value, you would divide the numerator by the denominator.

lar3ry:
As for converting the fraction… same way you do it manually. Assuming you want to convert it to a decimal value, you would divide the numerator by the denominator.

be sure to use float math (e.g. multiply by 1.0 first) otherwise all fraction might end up as zero.

Hi, why not convert it before you put it in, 20 3/4 = 20.75 simple maths. Where will you be getting these fraction type numbers rather than decimal from?

Tom...... :)

maybe because some fractions are more difficult e.g 17/27 to convert manually

robtillaart:
maybe because some fractions are more difficult e.g 17/27 to convert manually

My slide rule says 0.63 That’s close enough :smiley:

Hi Jimbo,

we learned at school how to do such from the head by means of approximations and multiplications (for double digits)

27 is 2 larger than 25 (1/12) ==> so 1/27 is about 1/12 or 8% smaller than 1/25 (which is 0.04)

==> 17/25 ==> 17 * 0.04 = 0.68

==> correction 8% of 0.68 = 0.08 * 0.68 = 0.054... ==> 0.68 - 0.054 = 0.626 => 0.63

Conclusion: your ruler is quite good :)

robtillaart: Conclusion: your ruler is quite good :)

Well I keep it fully charged Rob

How would I use a delimiter?

How would I use a delimiter?

The question itself contains 6 delimiters of two different types. So, clearly you already know how to use delimiters.

Therefore, the question that you asked is not the one that you meant to ask.

zwood14:
How would I use a delimiter?

Actually, PaulS’ answer was pretty good, in that without any delimiters, your question would read “HowwouldIuseadelimiter?”

I left the other delimiter in there, because a question mark in Emglish is more than just a delimiter. It also serves to make the preceding sentence a question.

Anyway, A delimiter is just a predefined character that cannot appear in the data part of the sentence (or packet, or value, or whatever you want to call it). So, if you have control over what is sent to the Arduino, you will want to allow the Arduino to figure out what it is you’re sending. That’s why I mentioned delimiters. In the case of a whole number and a fraction, all you need is two delimiters. The first will separate the whole number from the fraction, and the second will allow the Arduino to indicate the end of that combination of values. So…

2111/16 Would serve to input the value 21 11/64.
33/4 will serve to input the value 3 3/4.

zwood14:
I’m trying to think of the best way to input a number such as 20 3/4 in the serial port, and use it later in the program in a math function

Enter the value as a decimal instead. Then you can use standard parsing functions to process it to a float value you can use in your code.

Actually, PaulS' answer was pretty good, in that without any delimiters, your question would read "HowwouldIuseadelimiter?"

Actually, it would read "HowwouldIuseadelimiter". The punctuation at the end was the second type if delimiter.

Okay cool! I got the delimeter thing now haha

So I could just a Serial.parsefloat() to read the whole number and the fraction?

zwood14: So I could just a Serial.parsefloat() to read the whole number and the fraction?

You can if you enter the number as a decimal and not as a fraction.

You have to face it, computers don't do fractions. At some point you have to do the division and keep the number as a decimal. You can do that in your code, but you'll have to read the serial stream yourself. Or you can do the math yourself before you input the number and use parseFloat(). It's entirely up to you.

But if you want it to be entered as a fraction, you'll have to read three separate numbers, the whole part, the numerator, and the denominator, and then do the math to change it to a decimal in code.

Okay thanks!! I'm still new to programming.

I knew computers couldn't do fractions. I'm just trying to make it easier for the user.

How would I have the program read the three separate parts?

How would I have the program read the three separate parts?

With three calls to Serial.parseInt(), unless you seriously intend to support input like "21.7 13.6/298.4".

Okay,

so i can use two different serial.parseint() in the same function?

so i can use two different serial.parseint() in the same function?

Or three - one for the whole number, one for the numerator, and one for the denominator.

okay thanks,

I'll have to play with it. Because I'm not sure how to make it read three different numbers from the same input