426,060 Members | 1,878 Online Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

# How to check a given string is float-point in (c#) ?thanx

 P: n/a I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : [ws][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits][ws]) and the index is the location after "." in name. First, i should check the given name is a valid float-point, and then, to check the number in name in index locaion is > 0 or not? eg: round(123.45612, 2) is that the "5" is > 0 . I indeed need your help , can you give me some coder or some advice . appreciate very much. Nov 16 '05 #1
7 Replies

 P: n/a zjut wrote: First, i should check the given name is a valid float-point... All floating point types have a Parse method which converts the string representation of a number to the types equivalent. For example: double pi=double.Parse("3.14"); You should pass an IFormatProvider to this method to handle the different decimal points used in different cultures. to check the number in name in index locaion is > 0 or not? eg: round(123.45612, 2) is that the "5" is > 0 . My interpretation of your question is that you want to check if the numeric character on an index is greater than 0? If so you can do the following: bool isGreater=char.GetNumericValue("123.45612",5)>0; Anders NorÃ¥s http://dotnetjunkies.com/weblog/anoras/ Nov 16 '05 #2

 P: n/a Thanx very much. It is helpful to me.But it still cannot solve my problem.Because (0.1e1234 or 0.12e-450 or -.12345664544544 or 654564,4654,14.22222221432545414145515454464646) all the above string is allow input and they are valid . So according to ur advice, i can call Double.prase(..) to check the given string contain invalid char or not,if it throws FormatException, the given string is invalid, if it throws OverOfException, it is allowed, and i should parse it by other way(but the format is correct just over the flow i think) . And the GetNumericValue is very helpful to me. So can someone give me some advice again. Thanx ! "Anders NorÃ¥s [MCAD]" wrote: zjut wrote: First, i should check the given name is a valid float-point... All floating point types have a Parse method which converts the string representation of a number to the types equivalent. For example: double pi=double.Parse("3.14"); You should pass an IFormatProvider to this method to handle the different decimal points used in different cultures. to check the number in name in index locaion is > 0 or not? eg: round(123.45612, 2) is that the "5" is > 0 . My interpretation of your question is that you want to check if the numeric character on an index is greater than 0? If so you can do the following: bool isGreater=char.GetNumericValue("123.45612",5)>0; Anders NorÃ¥s http://dotnetjunkies.com/weblog/anoras/ Nov 16 '05 #3

 P: n/a "zjut" wrote: i can call Double.prase(..) to check the given string contain invalid char or not,if it throws FormatException, the given string is invalid, if it throws OverOfException, it is allowed, and i should parse it by other way No. Double.Parse *either* throws an exception, indicating that the format is invalid, *or* throws no exception and returns the value of the parsed string. try { double d = Double.Parse("123.45"); // d = 123.45 } catch { // ... invalid format ... } P. Nov 16 '05 #4

 P: n/a zjut wrote: Thanx very much. It is helpful to me.But it still cannot solve my problem.Because (0.1e1234 or 0.12e-450 or -.12345664544544 or 654564,4654,14.22222221432545414145515454464646) all the above string is allow input and they are valid . So according to ur advice, i can call Double.prase(..) to check the given string contain invalid char or not,if it throws FormatException, the given string is invalid, if it throws OverOfException, it is allowed, and i should parse it by other way(but the format is correct just over the flow i think) . Decimal is the largest floating point type in the .NET framework. You should use Decimal if you're handling large numbers. As you point out all of these numbers are valid, and they can be parsed using Decimal.Parse. However, 0.1e1234 and 0.12e-450 cause an overflow exception because they are too precice for the Decimal type. Do you really need your code to be this precice or are you using these numbers for the sake of the example? To parse numbers with exponents you must supply a NumberStyles parameter to the Parse method with the NumberStyles.AllowExponent flag set. In addition you should pass a CultureInfo instance since all of your example numbers are formatted according to a specific cultural convention. You can use this code to parse all of the numbers in your example, but the two first will cause an OverflowException. decimal.Parse("-.12345664544544",NumberStyles.Any,System.Globaliza tion.CultureInfo.InvariantCulture); Anders NorÃ¥s http://dotnetjunkies.com/weblog/anoras/ Nov 16 '05 #5

 P: n/a > Decimal is certainly the largest in terms of number of bits, but it doesn't handle the largest numbers. Decimal.MaxValue is 79,228,162,514,264,337,593,543,950,335. Double.MaxValue is 1.79769313486232e308 - considerably larger. Right. Maybe I could have been more precise. The number causing the overflow was a high precision floating point (0.1e1234). Anders Norås http://dotnetjunkies.com/weblog/anoras/ Nov 16 '05 #6

 P: n/a This is the worst situation,but it is allowed. The fallow is what i want to do --------------------------------------------------------------------------------------- /// /// Performs the rounding. /// Truncates everything past the accuracy digit, and rounds the last digit /// down. /// /// FormatException if number is not a valid floating-point number /// ArgumentNullException if number is null /// ArgumentException if comparisonDigit is less than 1 or greater than 9 /// the number to round /// the desired accuracy /// the comparison digit /// the rounded number public override string Round(string number, int accuracyDigit, int comparisonDigit) { try { Double db = Double.Parse(number); } catch (FormatException fe) { throw new ConfigurationException("The given number is invalid."); } catch (OverflowException ofe) { if (number.IndexOf("e") >= 0 &&number.Substring(number.IndexOf("."), number.IndexOf("e")).Length < accuracyDigit) // unfinishted { } } int index = number.IndexOf("."); string str = number.Substring(0, index + accuracyDigit); if (accuracyDigit == 0) { return number.StartsWith("-") ? Convert.ToString((Convert.ToInt32(str) - 1)) : str; } else { return number.StartsWith("-") ? str + Convert.ToString((Convert.ToInt32(number[index + accuracyDigit]) -47)) : number.Substring(0, index + accuracyDigit + 1) ; } } ------------------------------------------------------------------------ "Jon Skeet [C# MVP]" wrote: "Anders Nor?s [MCAD]" wrote: Thanx very much. It is helpful to me.But it still cannot solve my problem.Because (0.1e1234 or 0.12e-450 or -.12345664544544 or 654564,4654,14.22222221432545414145515454464646) all the above string is allow input and they are valid . So according to ur advice, i can call Double.prase(..) to check the given string contain invalid char or not,if it throws FormatException, the given string is invalid, if it throws OverOfException, it is allowed, and i should parse it by other way(but the format is correct just over the flow i think) . Decimal is the largest floating point type in the .NET framework. You should use Decimal if you're handling large numbers. Decimal is certainly the largest in terms of number of bits, but it doesn't handle the largest numbers. Decimal.MaxValue is 79,228,162,514,264,337,593,543,950,335. Double.MaxValue is 1.79769313486232e308 - considerably larger. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #7

 P: n/a zjut wrote: if the given number is 0.1e1234 and the accuracyDigit == 800, i need the check the 800th number. Maybe the given number is 0.1888...888. But i should check the given number is float-point (0.1212 or 122,12.2 > or 0.1e1234 etc) I think if i check it char by char ,it is the worst way. So if u can given me some suggestions, u will be appreciated and wellcomed. If your number cannot be parsed using neither decimal, for high precision numbers, nor double, for large numbers, you must either find a third party library or write your own floating point number library that is able to parse very-high precision or very-large numbers. To truncate the fractional part, convert the floating point number to a string, split the string at the decimal point and truncate the fractional part. If the first number after the end of the truncated string is equal to or larger than 5 add 1 to the truncated fractional part, otherwise leave it be. Writing a floating point parsing algorithm is beoynd the scope of what you can excpect from a newsgroup answer. Anders NorÃ¥s http://dotnetjunkies.com/weblog/anoras/ Nov 16 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion. 