472,331 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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

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 13834
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
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
"zjut" <zj**@discussions.microsoft.com> 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
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
> 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 Nors
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #6
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]" <an**********@objectware.no> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lodewijk Smit | last post by:
Hi, In the C standard of 1999 additional mathematical functions are added. For example, beside the traditional double sin(double x); there...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some...
7
by: geliangwei | last post by:
The 32-bit float number is defined by IEEE 754. Given a float variable, f, how to check whether each of the 32 bits is '1' or '0'? One possible...
0
by: Andrus | last post by:
I created RDL file containing TextBox with constant text. I used the following code to find TextBox <Widthtag value. However textbox width is too...
12
by: joestevens232 | last post by:
Okay, Im having some problems with my code. Im trying to use the <cstdlib> library and im trying to convert string data at each whitespace slot. I...
9
by: eggie5 | last post by:
How would I check if a string is a number? e.g: 'asdf2' =false '34' =true 'asf' =false '0' =true
10
by: james_027 | last post by:
hi, i have a function that I could like to call, but to make it more dynamic I am constructing a string first that could equivalent to the name...
5
by: hammer45 | last post by:
Modify payroll program so that it uses a class to store and retrive the employee's name, hourly rate and the number of hours worked. Use a...
10
by: Matthias | last post by:
Dear newsgroup. I want to write a template function which accepts either integer or floating point numbers. If a certain result is not a whole...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.