c# string is number code example

Example 1: c# how to check string is number

string s1 = "123";
string s2 = "abc";

bool isNumber = int.TryParse(s1, out int n); // returns true
isNumber = int.TryParse(s2, out int n); // returns false

Example 2: c# round number

double number = 1.5362
  
int rounded = Math.Round(number)
//rounds number to 2
  
double rounded_2 = Math.Round(number, 2)
//rounds number to 1.54

Example 3: is number c#

var isNumeric = int.TryParse("123", out int n);

Example 4: c# see if string is int

bool result = int.TryParse("123", out var n);