isinteger

How to check if a number is an integer in .NET?

北战南征 提交于 2020-06-28 03:59:09
问题 Say I've got a string which contains a number. I want to check if this number is an integer. Examples IsInteger("sss") => false IsInteger("123") => true IsInterger("123.45") =>false 回答1: You can use int.TryParse. It will return a bool if it can parse the string and set your out parameter to the value int val; if(int.TryParse(inputString, out val)) { //dosomething } 回答2: There are two immediate options that you can use. Option 1 - preferred - use Int32.TryParse. int res; Console.WriteLine(int

What is the most reliable way of checking if a floating point variable is an integer?

孤人 提交于 2020-01-03 01:55:06
问题 I can think of several ways, eg. Convert.ToInt32(floatingPoint) - floatingPoint == 0; Math.Truncate(floatingPoint) - floatingPoint == 0; floatingPoint % 1 == 0; Math.Floor(floatingPoint) == floatingPoint; //etc... But which method is most reliable? 回答1: You should not check for exact equality to zero, as a floating point number usually only contains the closest possible approximation to the number that you assigned to it. For example, the closest possible value to 42 that the type could

Number.isInteger(x) which is created can not work in IE

我与影子孤独终老i 提交于 2019-12-08 17:09:59
问题 Number.prototype.isInteger = Number.prototype.isInteger || function(x) { return (x ^ 0) === x; } console.log(Number.isInteger(1)); will throw error in IE10 browser 回答1: Apparently, IE treats DOM objects and Javascript objects separately, and you can't extend the DOM objects using Object.prototype. IE doesn't let you use a prototype that is not native.. You'll have to make a separate function (global if you want) as function isInteger(num) { return (num ^ 0) === num; } console.log(isInteger(1)