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. 7 13965
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/
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/
"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.
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/
> 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/
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
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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 are also the functions:
float sinf(float x);...
|
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 a time and
then check for the given string?
...
|
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 circumstances(CSV file source) but in very
similar...
|
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 solution is access f as a 32-bit int.
That is...
|
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 large.
How to find exact width of string for...
|
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 think if you see my code you'll get what im trying...
|
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
|
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 of the function I wish to call. how could I do...
|
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 constructor to initialize the employee information, and a...
|
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 number and if the template
parameter is an...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |