|
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 )
|
|
|
|
|
Another possibility is comething like this. Obviously this will destroy the input value, but if its needed it can be saved first.
int[] digit = new int[5]();<br />
<br />
for( int i = 4; i >= 0; --i )<br />
{<br />
digit[i] = value % 10;<br />
value /= 10;<br />
}
All the Best
Julian N.
-- modified at 9:09 Sunday 9th September, 2007
|
|
|
|
|
How to create textbox in c#..?? And how to set the tab space..??
|
|
|
|
|
You can drag it from the toolbox and drop it on the form to have it created automatically or you can create it like this:
TextBox mybox=new TextBox();
|
|
|
|
|
Hi
i want to drag item from listbox onto datagridview.
Can anybody provide coding 4 that....
Thanx in advance
|
|
|
|
|
hi
i want to send a message with "at commands" (and gsm modem).
i can send message and receive message.
and now that want to send a Unicode message (Arabic) can not !
can you guide me to send a Unicode with gsm modem and "at command" or any other command via serial port?
tanks
|
|
|
|
|
Have a look at this web page: http://www.scampers.org/steve/sms/ It has example of sending at messages and sending Unicode sms messages so it might help you
|
|
|
|
|
hi Giorgi Dalakishvili :
tanks so much.
your reference was very helpful for me.
|
|
|
|
|
You are welcome
|
|
|
|
|
Essentially, I want to press a key while running my application and have something happen.
Let's say I have Form with 3 TextBoxes named tb1, tb2, and tb3. What's the best way of setting things up so that when I press '1' tb1 is focused, '2' tb2 is focused, and '3' tb3 is focused. I know I can set up KeyPress events for tb1 AND tb2 AND tb3 to do this, but that seems very redundant. Is there a way where, no matter who currently has focus, I can press '1' and give tb1 focus? Thanks for the help, sorry, I'm new to C#.
|
|
|
|
|
hi
give each textbox eventhandler which uses form method form.OnKeyPressed(eventhandler) and then in form keypress event handle it
Pawel
|
|
|
|
|
You can set up an event handler for the forms KeyPress event. You should set Form.KeyPreview property to true in order for the form to get all the events first
|
|
|
|
|
Oh, I was missing that KeyPreview to true before... cool, I got it working, thanks so much!
|
|
|
|
|
You are welcome
|
|
|
|
|
hi
I want to write client/server programs that have interaction with each others. If client program want to access server what should I need? Just server ip and port number!
Should I have router ip or etc?!
|
|
|
|
|
hi
it depends. if you want to connect through internet you must have ip number of your computer(router) which connects to internet and direct (i don't know how it is called in english) ports used by program to your serwer.
If you want to connect through LAN serwer ip should be enough
Pawel
|
|
|
|
|
hi
I want to write client/server programs that have interaction with each others. If client program want to access server what should I need? Just ip and port number!
Should I have router ip or etc?!
|
|
|
|
|
|
In C#, I'm trying to read a text file into a combo box and use it as the items. When I try to do it, I get the newline characters and it's all smashed into one line. Any ideas or example on how to do this would be appreciated.
|
|
|
|
|
How are you doing it ? File.ReadAllLines will give you a string array, which is a viable data source for a combobox.
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 was goofing with StreamReader, this worked fine!
Thanks!
|
|
|
|
|
Hi
I have to Marshal a class which is holding a collection , How to achieve this??Is there any way to Marshal a collection in a class,I know that string, bool having MarshalAs methods, but collection dont have ??
prompt reply is appreciated as it is quite urgent..
Thanks
|
|
|
|
|
Hi
I want to populate a propertygrid passing treeview node argument in WPF.Please advise..I am new to WPF.
Thanks
|
|
|
|
|
Hi all,
Currently am working in a project, where in my application there is a window form, holding several pages(using WPF).now i've to implement session timeout functionality in my application using window hook for keyboard and mouse. i've a implemented a timer(System.Threading)TimerCallback, which periodically examine, whether, any key board message or mouse message occure ONLY ON MY APPLICATION. this means, minimizing my application form, i can play computer game, but after certain period my application can throw timeout session.
now the problem is, delegate(by which i'm calling window hook function whenever key or mouse pressed) is firing event even if my function is minimized. (i know this will fire), but the only requirement is how i manipulate these messages (key and mouse messages)only haapen to my application window. i don't know how to do this.. please help
please go through these urls, as i'm trying to develop window hook, by help of these.....
here he has used WH_KEYBOARD_LL Hook which is global
1. "http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx"
2. "http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx"
Please please help......
-- modified at 18:14 Saturday 8th September, 2007
kamalesh
|
|
|
|