String conversion C# [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert from scientific notation string to float in C#
Is it there an build-in function which converts string in format "2.71e+006" to a number or I have to write my custom algorithm?

The Decimal Parse method has an overload you can use:
decimal d = Decimal.Parse("2.71e+006", System.Globalization.NumberStyles.Float);
You can also do the same for a Double.

Related

format decimal number using ToString [duplicate]

This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx

Convert decimal (2.75) to time (2:45) [duplicate]

This question already has answers here:
C# String Format for hours and minutes from decimal
(3 answers)
Closed 9 years ago.
If I have a double like 2.75, is there a way in .Net to format it as '2:45'
If it is for example, 2.75555555555, it should round it to the nearest minute.
I would not mind coding this myself, but I am wondering if .Net can. I checked ToString but did not find anything.
Thanks
Use TimeSpan and its ToString formatter:
TimeSpan timespan = TimeSpan.FromHours(2.75);
string output = timespan.ToString("h\\:mm");
For example
TimeSpan.FromHours(2.75555).ToString("h\\:mm")
outputs
2:45

1E-08 to decimal [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse a Number from Exponential Notation
Does Decimal.Parse() support scientific notation?
I am trying to convert such values as 1E-08 to a decimal in C# because decimals are the preffered datatype for handling funds yet I get an error upon decimal.Parse() "Input string was not in a correct format." wouldn't converting to float first and then to decimal defeat the purpose?
Yes, converting to float would indeed defeat the purpose. The good thing is, you don't have to do that here!
You can use an overload for Parse that takes a NumberStyles specifier:
decimal d = decimal.Parse("1E-08",
System.Globalization.NumberStyles.AllowExponent);
Of course, if you are merely specifying a hard-coded decimal, you can use the decimal literal format:
decimal d = 1E-08M;
You can try with :-
decimal x = decimal.Parse("1E-08", NumberStyles.Float);

Does Decimal.Parse() support scientific notation? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# Parse a Number from Exponential Notation
I currently have:
decimal value = Decimal.Parse(dataRow["column"].ToString());
There is some data in the backend that is in scientific notation (ie 3.2661758893885E-05) and which is causing a FormatException on the parse. Is there an easy way to do this?
Try something like this:
Decimal.Parse(strExpression, System.Globalization.NumberStyles.AllowExponent));

How to Convert Hex String to Hex Number [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How do I convert hex string into signed integer?
example:"3A" convert to 0x3A,thanks a lot!
Convert.ToInt32("3A", 16)
int.Parse("3A",NumberStyles.HexNumber)
or
long.Parse("3A",NumberStyles.HexNumber)
etc...

Categories