String to double C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

25th Jul 2022, 7:22 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
17 Answers
+ 2
Tried to solve it and add comments for you. I hope this might help you https://code.sololearn.com/cZN1nIvKbaLw/?ref=app
26th Jul 2022, 11:21 PM
Mahad Ahmed
Mahad Ahmed - avatar
+ 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.
25th Jul 2022, 7:52 AM
Ipang
+ 1
It's indeed a pro one. Thank you for answering
25th Jul 2022, 8:15 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
25th Jul 2022, 8:46 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
+ 1
Again, thank you so much for putting your time in helping me.
25th Jul 2022, 8:57 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
+ 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.
26th Jul 2022, 12:44 PM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
+ 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.
26th Jul 2022, 11:47 PM
Ipang
+ 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.
27th Jul 2022, 6:25 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
+ 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.
27th Jul 2022, 6:26 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
0
It's still not turning the strings into doubles :/ If I enter (string)100.25, it returns (double)10025
25th Jul 2022, 8:18 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
0
Give me the string input example and the expected final output (savings), I'll see if I could find something ...
25th Jul 2022, 8:20 AM
Ipang
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 😅
25th Jul 2022, 8:25 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
0
Oh now I see why you go with priceInclTax.Length - 1. The most expensive product isn't included in the calculation ...
25th Jul 2022, 8:32 AM
Ipang
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.
25th Jul 2022, 8:41 AM
Ipang
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?
25th Jul 2022, 8:51 AM
Ipang
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 🧐
25th Jul 2022, 8:57 AM
Hannelore Van de Calseyde
Hannelore Van de Calseyde - avatar
0
Post an update here when you found the solution, might help someone who deals with the same issue in the future 👍
25th Jul 2022, 10:59 AM
Ipang