PC Review


Reply
Thread Tools Rate Thread

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

 
 
=?Utf-8?B?emp1dA==?=
Guest
Posts: n/a
 
      6th Jan 2005
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.
 
Reply With Quote
 
 
 
 
=?UTF-8?B?IkFuZGVycyBOb3LDpXMgW01DQURdIg==?=
Guest
Posts: n/a
 
      6th Jan 2005
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/
 
Reply With Quote
 
=?Utf-8?B?emp1dA==?=
Guest
Posts: n/a
 
      6th Jan 2005
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/
>

 
Reply With Quote
 
Paul E Collins
Guest
Posts: n/a
 
      6th Jan 2005
"zjut" <(E-Mail Removed)> 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.


 
Reply With Quote
 
=?UTF-8?B?IkFuZGVycyBOb3LDpXMgW01DQURdIg==?=
Guest
Posts: n/a
 
      6th Jan 2005
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.Globalization.CultureInfo.InvariantCulture);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th Jan 2005
"Anders Nor?s [MCAD]" <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Anders_Nor=E5s_=5BMCAD=5D=22?=
Guest
Posts: n/a
 
      6th Jan 2005
> 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/
 
Reply With Quote
 
=?Utf-8?B?emp1dA==?=
Guest
Posts: n/a
 
      7th Jan 2005
This is the worst situation,but it is allowed. The fallow is what i want to do
---------------------------------------------------------------------------------------
/// <summary>
/// Performs the rounding.
/// Truncates everything past the accuracy digit, and rounds the last digit
/// down.
/// </summary>
/// <exception>FormatException if number is not a valid floating-point
number</exception>
/// <exception>ArgumentNullException if number is null</exception>
/// <exception>ArgumentException if comparisonDigit is less than 1 or
greater than 9</exception>
/// <param name="number">the number to round</param>
/// <param name="accuracyDigit">the desired accuracy</param>
/// <param name="comparisonDigit">the comparison digit</param>
/// <returns>the rounded number</returns>
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]" <(E-Mail Removed)> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
=?UTF-8?B?IkFuZGVycyBOb3LDpXMgW01DQURdIg==?=
Guest
Posts: n/a
 
      10th Jan 2005
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Float point variable comparison issue? Fritz Microsoft C# .NET 1 16th Feb 2011 11:47 PM
storing floating point data with using float or double Steve Microsoft C# .NET 6 5th May 2006 07:13 PM
Converting a float to a string eric.goforth@gmail.com Microsoft C# .NET 5 22nd Mar 2006 02:16 AM
String to Float Dave Microsoft C# .NET 5 20th Nov 2004 12:38 AM
Conversions between float and string Gustaf Liljegren Microsoft C# .NET 5 19th Feb 2004 05:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:26 AM.