Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new to coding and need to write a program for C#
the goal is to write a program that prompts the user to enter an amount including decimals. and the program gives the user back the remander as money. ie...
quater dimes nickle pennie
First you want to get the users input and store it in a variable.
The input will be text so you have to convert that to a decimal.
Then you will need to do your calculations. That is probably the part you need help on.
Lets say the user entered in 25 dollars and 43 cents - 25.43.
I would first divide the dollars by 4 to get the number or quarters.
Then go from there. You will likely use mod %.
Have fun, im sure you can figure it out if you try.
I think the problem isn't with your int.Parse, but with the way you were concatenating your Console.WriteLine string. Here's the fix, I'm not sure if that's what you were needing.
When you concatenate object to a string, add {}. Starting with the number 0 add those inside depending on how many object you want to add to the string like so: {0}. Once you're done with the string you can add the objects after the it separated by a comma.
int dollar = 100;
decimal quarter = .25m;
decimal dime = .10m;
decimal pennie = .01m;
Console.WriteLine("tell me how much money you have, make sure you include doolars and cents. ");
string userMoney = Console.ReadLine();
double userMoney1 = double.Parse(userMoney);
Console.WriteLine("that equals, {0} ", userMoney);
Console.ReadLine();
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am making a program (in school for intro to computer programming(dont worry, this is all extra credit stuff, not asking for cheat answers)) that will take a user decided set of user inputs and save them all to do math (more specifically, finding the median, but I want to figure that out myself) on them. I am fairly sure that I need an array to do this (even my teacher hinted that that is what you needed to do).
My plan is to have a variable x that will decide the amount of separate numbers in the array (will not only be the number in the array, but also the number to check how many times I want to run the loop for asking for numbers), and then to have that many user inputted numbers inputting by the user, and then to be able to take those numbers and find the median of them (I will probably have to check if the number is even or odd first, then sort the numbers (somehow), then find the middlemost number.)
Thanks!
Instead of using array which has fixed size, try using List.
List allows you to add indefinitely many (until it fits into memory) elements.
So, in your for loop you could read user input and add it to the list.
After that you can sort entire list using sort method and return middle element, which is a median.
It could look like that:
int n = //TODO: read number of user inputs
List<int> elems = new List<int> ();
for (int i = 0; i < n; i++) {
int x = // get input
elems.Add(x);
}
Console.WriteLine(elems[elems.Length / 2]);
You can take a look at the List<T> class here for storing the numbers.
For reading input, see Console.ReadLine.
For converting string to int, take a look at int.TryParse.
I gave you the tools, now start coding! :D
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm a newbie in C# programming and I need your help on this one.I have a string with a value of "12:45:00" and I would like to convert it into decimal with two decimal places.
Though you can actually use DateTime struct for your case, but since your string does not contain date info, I think the easiest way would be to use string.Split instead.
string[] strs = "12:45:00".Split(':'); //will give you 12, 45, and 00
double val = Convert.ToDouble(strs[0]) + Convert.ToDouble(strs[1]) / 60 + Convert.ToDouble(strs[2]) / 3600;
Then to print it in two decimal values, simply do:
val.ToString("F2"); //you will get 12.75
Or, to get 12.45, then simply do:
double val = Convert.ToDouble(strs[0]) + Convert.ToDouble(strs[1]) / 100; //note 100 here - second doesn't matter here
If your intention is to convert a duration in hours, minutes and (optionally) seconds into hours, you can do that like so:
double answer = TimeSpan.Parse("12:45:00", CultureInfo.InvariantCulture).TotalHours;
Console.WriteLine(answer); // Prints 12.75
This is the value that you can use along with an hourly rate to calculate a gross income.
Note: It would be incorrect to convert 12:45 (hh:mm) into 12.45, because 12:45 is 12.75 hours, not 12.45 hours.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a way to take phone numbers Input from an end user, given the various ways that can be formatted, and reformat it in a more standardized way. My end users work with only US phone numbers. Looking at the C# port of google's LibPhoneNumber, I assume it was made for exactly this task, but I can't figure out its usage.
My goals for output is to standardize on 999-999-9999 format.
So if the user inputs (516)666-1234, I want to output 516-666-1234.
If the users inputs 5166661234, I want to output 516-666-1234.
If possible, if the user inputs (516)666-1234x102, I'd like the output to be 516-666-1234x102
If the library can't handle extensions, I'll deal with that problem externally.
What calls do I have to make to produce the desired output?
Alternatively, can you provide a link that provides the answer to my question?
You have to combine RegEx (to string out non-numeric fields) and string formatting. Note that string.Format of a number is tricky, so we format the 10-digit phone number and then append the extension.
public string getPhoneNumber(string phone) {
string result = System.Text.RegularExpressions.Regex.Replace(phone, #"[^0-9]+", string.Empty);
if (result.Length <= 10) {
return Double.Parse(result).ToString("###-###-####");
} else {
return Double.Parse(result.Substring(0,10)).ToString("###-###-####") +
"x " + result.Substring(10, result.Length-10);
}
}
To do this right, I'd want to check for "1" at the start of my digits and ditch it. I'd want to check for < 10 characters and make some assumptions about area code, etc.
But - this is a good start.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a situation on my C programming here and just wondering whether my solution is the correct way:
I have a LED display with particle count sensor and will show 6 digit of seven segment numbers as the count value. The sensor will give voltage input value. The input is from 0V to 10V. So the range of 0V-10V need to be shown in the display as 000000 to 999999 count.
My solution is:
Display number = Input voltage * 99999.9
For example:
Display number = 10.000*99999.9=999999
Display number = 5.500*99999.9=549999
Display number = 2.300*99999.9=229999
Is this the correct solution? I notice that I will get a lot of 9 on the display value.
The most usable and user friendly solution is to ignore the fact that your most significant digit is capable of displaying up to 9 and simply multiply by 10000 unless you desperately need the maxim resolution in which case simply use a scale factor of 100000 and document that your range is 0-9.99999.
My reasoning is that it is better to either loose one digit in the accuracy across the whole range or clip just the maximum value than to have an error across the entire range.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know if you got me what I'm trying to explain... (English is not my main language).
Ok here, I got this string 590CBC145FA and this one F6EC5CA9A and the only part that I want, no matter how long is the string, is the last eight char like this: CBC145FA 6EC5CA9A. The 590 and F are obsolete to me, like: !S!ZV)+D_?CEFZEZAF = CEFZ!Z#F.
I tried to use serial.Substring(3); only work for the first one: 590CBC145FA = CBC145FA
If I try to use serial.Substring(1); it work for F6EC5CA9A = 6EC5CA9A
But what happen if I get a string very long and I don't know how much the obsolete part is... I can't use serial.Substring(X); because I don't, like I said, how long is the obsolete part.
I only want to get the eight last char of any serial no matter how long is.
You want to get the eight last char of any serial no matter how long is.
string test = "F6EC5CA9A";
string result = test;
if (test.Length >= 8)
result = test.Substring(test.Length-8);
You just need to discover the index of the 8th character before the end of the string. And this could be easily calculated using the Length property of every string.
Of course you need to be sure that your string is at least 8 characters. You don't say what do you want in case this length is less than 8, so I assume that you want the original input back