This question already has answers here:
Problem parsing currency text to decimal type
(4 answers)
Closed 8 years ago.
We use following code to convert a decimal to a string,
td_1.InnerText = String.Format("{0:C}", price)
so the result could be any of following cases:
4.83 -> $4.83
0 -> $0.00
-30.24 -> ($30.24)
my question is if there any way I can reverse the string back to a signed decimal? For example:
$4.83 ->4.83
($30.24) -> -30.24
I tried decimal.tryparse, doesn't seem work. Here is my code
decimal number;
if (Decimal.TryParse(price, out number) && number <0)
...
Have you tried adding NumberStyles, like:
decimal.Parse(currencyValue, NumberStyles.Currency);
Related
This question already has answers here:
How to convert Decimal to ASCII in c#.net?
(3 answers)
Closed 1 year ago.
I have got the string like in hex view
33-35-34-37-36-32-31-31-32-35-32-31-32-38-38
I would like to get it like a string of corresponding values
354762112521288
I see it could be done like here
So I am wondering which methods I have to use for it?
I try this for the first value is 33
string hex = "0x33";
int intValue = Convert.ToInt32(hex, 16);
but It gives me 51 instead of 3.
Thanks for helping!
I think,
The hex value for 3 is "3"
"0x33" is 51 in decimal
This question already has answers here:
Math.Round vs String.Format
(6 answers)
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 5 years ago.
I'm recovering the value of a website, however I'm having difficulty formatting the string ...
My Value: $314.623230
Expected result: 314.62
How i can archive this result?
I have tried to convert the string to decimal and use ToSting() with arguments (N0, N1, 0:0##), but it does not return the expected result ...
var usCulture = new CultureInfo("en-US");
decimal d = decimal.Parse("$314.623230", NumberStyles.Currency, usCulture);
string expectedResult = d.ToString("0.##", usCulture); // 314.62
This question already has an answer here:
C# how to convert a double to string with exponential notation
(1 answer)
Closed 6 years ago.
How to convert double number to exponential notation in c#?
My number
I would like number will look like:
-1.6500000000000000e1
I looks on the article:
C# how to convert a double to string with exponential notation
But this didn't fully answer me:
number.ToString("e16", CultureInfo.InvariantCulture)
provide me number looks like:
-1.6500000000000000e+001
I would like at the end only e1 for non negative, or e-1 for negative
Thanks!
Try using formatting:
Double value = -1.6500000000000000e1;
// e15 - exponential form, small "e" for exponent, 16 digits
String result = value.ToString("e16", CultureInfo.InvariantCulture);
Console.Write(result);
Edit: in case you want "e1" form of exponent, you should specify it like this:
String result = value.ToString("0.0000000000000000e0", CultureInfo.InvariantCulture);
This question already has answers here:
How to convert this scientific notation to decimal?
(6 answers)
Closed 8 years ago.
In my application I get a number 2.424242E+08 in my label called lbl_price and when i try to convert it to a decimal i get a FormatEception. How can i fix this?
decimal newVat = Convert.ToDecimal(lbl_price.text);
Try:
decimal newVat = decimal.Parse(lbl_price.text, System.Globalization.NumberStyles.Float);
This question already has answers here:
Truncate Decimal number not Round Off [duplicate]
(10 answers)
Closed 9 years ago.
I have a decimal number :
decimal a = 0.8537056986486486486486486486;
I want to show it as string with only 8 digit after point :
a equals -> "0.85370569".
How can I do it ?
For example, like this:
a.ToString("0.00000000");
Try using
a.ToString("D8");
This should convert it to the correct format.
Edit:
As said in the comments, D is only used for integral types, thus not for decimals and such.
Use
static void Main(string[] args)
{
decimal d = (decimal)0.8537056986486486486486486486;
Console.WriteLine(d.ToString("N8"));
Console.ReadLine();
}
instead.
This will format it to 0.85370570
To perform this numerically you could use:
decimal a = (decimal) 0.8537056986486486486486486486;
String test = (Math.Truncate(100000000 * a) / 100000000).ToString();
decimal a = 0.8537056986486486486486486486;
var v= a.ToString("#.########");