How to display # using / and %
-
:)I am new to programming and just started learning C#. I am using Visual C# 2005. I am working on a project where the user enters 5 digit # and then I need the digits to display separated by a space. This is a console application. I got as far as asking to enter # then displaying the digits by using / and %. I am having a problem showing the 2nd, 3rd and 4th #. How do you get the just the 1st digit to the right of the decimal to show. I used for example: digit1 = ( value1 / 10000); digit2 = ( value1 % 10000); digit3 = ( value1 % 1000); digit4 = ( value1 % 100); digit5 = ( value1 % 10); I am having a problem with the 2nd, 3rd and 4th to display only the first digit to the right of the decimal. All have been declared int..any help is appreciated, thanks Thanks. Hope my post is okay with detail if not let me know, this is my first post
-
:)I am new to programming and just started learning C#. I am using Visual C# 2005. I am working on a project where the user enters 5 digit # and then I need the digits to display separated by a space. This is a console application. I got as far as asking to enter # then displaying the digits by using / and %. I am having a problem showing the 2nd, 3rd and 4th #. How do you get the just the 1st digit to the right of the decimal to show. I used for example: digit1 = ( value1 / 10000); digit2 = ( value1 % 10000); digit3 = ( value1 % 1000); digit4 = ( value1 % 100); digit5 = ( value1 % 10); I am having a problem with the 2nd, 3rd and 4th to display only the first digit to the right of the decimal. All have been declared int..any help is appreciated, thanks Thanks. Hope my post is okay with detail if not let me know, this is my first post
Hi - welcome to Code Project. digit2 = value1 % 10000 will give you the remainder when dividing by 10000. In essence, this means it will give you the last four digits. To get just the one digit, you'd also have to strip to the right, so something like this should work Math.Floor((value1 % 10000)/1000) The mod (%) strips values to the left, then the divide strips to the right. Finally, Math.Floor stops a result such as 3.6 from rounding up. The other approach is to do this: string number = value1.ToString(); digit1 = int.Parse(number[0]); digit2 = int.Parse(number[1]); etc You don't need to use int.TryParse or char.IsDigit, as you know the values are all numbers. You DO have to make sure your number is 5 digits long, I'd be inclined to store the values in a list or an array, then you can write code that works with any length number.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
:)I am new to programming and just started learning C#. I am using Visual C# 2005. I am working on a project where the user enters 5 digit # and then I need the digits to display separated by a space. This is a console application. I got as far as asking to enter # then displaying the digits by using / and %. I am having a problem showing the 2nd, 3rd and 4th #. How do you get the just the 1st digit to the right of the decimal to show. I used for example: digit1 = ( value1 / 10000); digit2 = ( value1 % 10000); digit3 = ( value1 % 1000); digit4 = ( value1 % 100); digit5 = ( value1 % 10); I am having a problem with the 2nd, 3rd and 4th to display only the first digit to the right of the decimal. All have been declared int..any help is appreciated, thanks Thanks. Hope my post is okay with detail if not let me know, this is my first post
Another possibility is comething like this. Obviously this will destroy the input value, but if its needed it can be saved first.
// value is the 5-digit input value int[] digit = new int[5](); for( int i = 4; i >= 0; --i ) { digit[i] = value % 10; value /= 10; }
All the Best Julian N. -- modified at 9:09 Sunday 9th September, 2007