Question Limit display characters of variable

Griever

New member
Joined
Jul 26, 2015
Messages
2
Programming Experience
Beginner
How to Limit display characters of a variable, in MessageBox.Show? somebody know?

Example:

int val = 256893;

MessageBox.Show("Show the numbers: "+val);

I want to display only the numbers 256.

Thank You!
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,183
Location
Norway
Programming Experience
10+
Convert the value to string with ToString method, then use Substring method to extract the part of string you need.
 

zerocool

Member
Joined
Aug 30, 2015
Messages
5
Programming Experience
3-5
Hello,

I'm new to the forum and learning C#. So I'm probably going to take some blasting over the next few months for not having clean code. But I figured a good way to get good at C# is to practice. I thought maybe I could look and form post and try to solve them and that would not only help the poster but also myself.

Here is what I came up with for your question.

public static void Main(string[] args)
{
int i = 256893; //original int
string s = i.ToString(); //convert it to a string
s = s.Substring(0,3); //substring it out starting at 0 index and ending 3 chars in

Console.WriteLine(s);

}
 
Top Bottom