how do i display how many digits the number has? [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY

You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code

string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.

Related

c# adding money together removes the 0 at the end [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In c# when adding two decimals the program will automatically get rid of the number 0 after the decimal places.
For example adding 0.50 to 1.20 will produce the answer of 1.7 and this is annoying because i need to display this answer in terms of money.
Is there a way to prevent this?
If you want to display your Decimal with two decimal places, please use :
myDecimal.ToString("N2");
You may want to take a look at Standard Numeric Format Strings for more information.
decimal d = 0.50m;
decimal d1 = 1.20m;
Console.Write(d+d1);
Please find this Post
I'm not sure about if you mean this, but you can try the toString() method in currency format this way:
double number = 1.2;
string numberCurrency = number.ToString("C");
Console.WriteLine(numberCurrency); //this prints "1.20"
I recommend you to read this https://msdn.microsoft.com/es-es/library/kfsatb94(v=vs.110).aspx

Generating folders using textboxes [closed]

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
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck

Regular Expression for a code that starts with specific letters [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How can I build a regular expression that will check this code:
ABC00000
The ABC is fixed and the 0 is a place holder for numbers.
The maximum length of numbers is 5.
Use this Regex:
ABC(\d{5})
The capturing group will also help you to retrieve the number after 'ABC', if you need it.
A non-regex way:
testString = "ABC00000";
if (testString.StartsWith("ABC") && int.TryParse(testString.Substring(3), 0))
{
}
The above code basically checks if the first three characters are 'ABC' and the last 5 characters are numeric. The int.TryParse() function returns if the number is parse-able from the string, i.e., if it is a number.

What does string.IndexOf do? [closed]

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'm working through string manipulation, where I need to find characters or copy some part of the string that the user has input and divide it to 3 different areas. I'm not asking anything here about code, I'll do it myself, but I was searching in the documentation that Microsoft provide about the IndexOf method and its 8 overloads and I can't really understand how to apply it. I simply can't understand what it is supposed to do.
Returns the first appearance of a specified char.
For example
string x = "Hello World";
x.indexOf("W");
it will return 6 (0 based count).
The overloads let you choose for example, where you want to start searching.. like
x.indexOf("W", 7); it will return -1 because W is at position 6 so if starts at 7 it won't find any.
I hope this helps ! the best way is to play with it
This also works with arrays.
I believe the MSDN explanation is pretty clear.
For example:
string something = "something";
int indexOfT = something.IndexOf("t"); // => returns 4
Reports the zero-based index of the first occurrence of the specified string in this instance.
So if "t" is in the fifth position of "something", 4 would be it's zero-based index.

Adding two different digit Numbers in c# ( without using BigInteger) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have a Task to do C#. I need to add two numbers.
The first number contains around 100 digits like "12822429847264872649624264924626466826446692............"
and second number also with 100 digits or more or less
by using this numbers i need task like add/sub/multiply/div
I done this using BigInteger in C#
But do I need to do this using arrays or strings?
Since they are both 100 digits just start with the last digit and in a for loop just add each one, but if the value is > 10 then remember to add one to the next digit.
This is how children learn to add, you just need to follow the same steps, but the answer should be in an array of 101 characters.
UPDATE:
Since you have shown some code now, it helps.
First, don't duplicate the code based on if str1 or str2 is larger, but make a function with that logic and pass in the larger one as the first parameter.
Determine the largest size and make certain the smaller value is also the same size, to make math easier.
The smaller one should have leading zeroes (padding), again to help keep the code simple.
You can also start by looking at the source code for structures such as BigInteger. They would provide you more insight into aspects such as computational efficiency and storage, particularly about multiplication and division. You can take a look at here or here.

Categories

Resources