Tuesday, July 28, 2009

Is there any datatype available in C# Language which be suitable for any input numeric form(int,float,double)?

My Input My be float or numeric or int and i wanna access them without lose and data or change there form??

Is there any datatype available in C# Language which be suitable for any input numeric form(int,float,double)?
No number type in C# will accommodate ANY numeric input. Double will do a lot, but you'll have a problem past a certain number of digits. MS says the approx range is ±5.0 × 10^−324 to ±1.7 × 10^308





A double will drop the least significant digits of the entered number if it can't be expressed in 64 bits, so an entry of "123456789.123456789" parses to 123456789.123457





As a floating-point entry, double is ALWAYS an approximation, not exact - remember that too.





If the accuracy is extremely important than you will have to write your own class.
Reply:use double.
Reply:I would simply use double. It will handle all types and will not truncate your data or cause a loss.





See my example:











using System;


public class IntFloatDoubleDecValues


{


static void Main()


{


int firstInt, secondInt;


float firstFloat, secondFloat;


double firstDouble, secondDouble;


decimal firstDecimal, secondDecimal;





firstInt = 17;


secondInt = 4;


firstFloat = 17;


secondFloat = 4;


firstDouble = 17;


secondDouble = 4;


firstDecimal = 17;


secondDecimal = 4;


Console.WriteLine("Integer:\t{0}\nfloat:...


firstInt/secondInt, firstFloat/secondFloat);


Console.WriteLine("double:\t\t{0}\ndecim...


firstDouble/secondDouble, firstDecimal/secondDecimal);


Console.WriteLine(


"\nRemainder(modulus) from integer division:\t{0}",


firstInt%secondInt);





}


}


No comments:

Post a Comment