0
String to double C#
https://code.sololearn.com/cUxcljBIllYj/?ref=app For the Super Sale practice exercise I've been trying to convert a string to a double. This is giving me some odd outcomes (80.99 becomes 0, for example). I tried both Convert.ToDouble and Double.Parse. both give the same result. What am I doing wrong?
17 Antworten
+ 2
Tried to solve it and add comments for you. I hope this might help you
https://code.sololearn.com/cZN1nIvKbaLw/?ref=app
+ 1
Haven't tried that one, it's for PRO only?
Line. 24:
Redundant increment of loop counter <i>. Loop counter is already incremented in loop construct. Delete the line.
Line 43:
Improper loop condition, it should be
for (int i = 0; i < priceInclTax.Length; i++)
Adjust loop condition proportionally.
+ 1
It's indeed a pro one. Thank you for answering
+ 1
Here you go:
https://code.sololearn.com/ce62sazr38E0/?ref=app
+ 1
Again, thank you so much for putting your time in helping me.
+ 1
The problem, it seems, is in the numbers being written with a dot and not a comma.
If I use only a space to split the numbers and write them with a comma (so: 100,25 80,99 40,00) the code works in Visual Studio as well.
+ 1
About the issue with comma, it *probably* means that your computer and SoloLearn's server are using different localisation settings. The settings that defines a certain character that is used as the thousand separator and digit grouping in numbers.
+ 1
@MahadAhmed
Thank you. I'm reading up on Linq now. I'm a beginner and didn't know that was possible. Same with the Array slicing.
+ 1
@Ipang
That might indeed be right. I'm in Europe and given the use of dollars in the exercises, I'm guessing Sololearn is American.
0
It's still not turning the strings into doubles :/ If I enter (string)100.25, it returns (double)10025
0
Give me the string input example and the expected final output (savings), I'll see if I could find something ...
0
The idea is that you get 30% discount on all items except the most expensive one. You give the amount after the comma of your discount as tip and they request how much you saved.
Input: 100.25,80.99,40.00
Output: 38
I haven't tested the actual math at the end yet given that I can't get past the string to double issue 😅
0
Oh now I see why you go with priceInclTax.Length - 1. The most expensive product isn't included in the calculation ...
0
Can you post link to the updated version of the code? there's gotta be something overlooked there.
Having applied the fix for line 24 and 43, I tested the code with such input in your original code and got 38 as output.
0
Okay, so for input 100.25,80.99,40.00 I got these as output
100.25
80.99
40
38
Where did it go wrong?
0
This is absolutely weird. I've been writing this code in Visual Studios (where it doesn't work) but when you run it in the app it's perfectly fine 🧐
0
Post an update here when you found the solution, might help someone who deals with the same issue in the future 👍