Can't turn string into a double SOMEONE HELP ME
For some reason line 38 returns an error. The user is expected to enter the price of an item on a 16-key pad. Press * to accept, # to decline. It's expected to convert the price thing to a double to add to the total, but why it aint working?



17 Replies
Be a lot easier to read text than pictures
OK...
@DarwinWasW
https://docs.arduino.cc/language-reference/en/variables/data-types/stringObject/
You can't just cast a string to double. You need to use the .toFloat() method
@MinecraftFan69420
And then turn that float to a double?
oh no
just use .toDouble() then 😆
https://docs.arduino.cc/language-reference/en/variables/data-types/stringObject/Functions/toDouble
Oh thank you!
double is just a larger float
btw this might be a bit advanced for you but just saying... computers (and arduinos) are actually kinda bad at math. When you do maths with doubles, you can sometimes get wrong results. For example
0.1 + 0.2
should be 0.3
but the arduino will actually give you 0.30000000000000004
.
Might not matter at all for your use case. But if you do more math with the prices, then you might run into issues
Idea:
Don't use floating point maths, don't use floating point datatypes. So no float and no double.
Computers are actually great at maths. Just not at floating point math.
So you can just use int and you won't have any of those problems. But int can't do decimal numbers, right? You don't actually need decimal numbers for prices! Just use cent! int price
could be the price in cents. Then you just add the decimal when you display it to the userSo we put the total as an int for c (cents)?
And then convert it to a string
And then add a decimal at the third-to-last thing
Something like that
yes
@PenPengu Cool
Store the prices and everything as int and use cent.
When you want to display it to the user:
Convert the int to string.
Add a decimal
.
before the last two digits.
basically
Again, this might not matter at all in your use case but could still be interesting to think about 😆:
Using int instead of double can be way faster, and make your program smaller.OK, thx!
It's also possible to store things as integer
dollar
s and cent
s, which might make printing easier, but you need to be careful to make sure you don't have 51
dollars and 105
cents or somethingIf you want to learn more about floating point math and why it's the way it is (it all is to do with converting decimal numbers to floating point and back), then here's some resources:
https://0.30000000000000004.com/
https://www.youtube.com/watch?v=9hdFG2GcNuA
UncommentatedPannen
YouTube
Floats
I explain how floats work, also known as floating point numbers. Floats are a type of variable, which are used to approximate real numbers. Other types of variables, such as bytes, shorts, and longs, can only represent integers. So in SM64, those are used to represent Mario's lives, Mario's coin count, Mario's HP, angles, timers, vertices of lev...
The tl;dr of it is to imagine you were asked to do calculations with
1/3
but you were forbidden to use fractions anywhere. No matter how many decimal places you use, you can't perfectly represent 1/3
in decimal.
Floating point is the same but with 1/10
in binary being a repeating decimalI didn't watch it yet. But thanks so much! I didn't know how to explain it well and couldn't find a good link right now
it's a video I like linking because
- it hits coarseness, repeating digits, type casting all in 1 video
- is reasonably beginner friendly
and of course, sm64