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("#.########");
Related
This question already has answers here:
How remove decimal part from string?
(3 answers)
Closed 1 year ago.
I want to remove decimal point from string.
I try with this code but not work for me:
string wSpeed = Math.Round(weatherBindingData.WeatherDataCurrent.Wind.Speed) * 3.6 + "km/h";
WindSpeed.Text = wSpeed;
I want to remove the decimal point, but with this code I receive a values with decimal point.
How is the correct way to remove the decimal point?
Use formatting:
string wSpeed = $"{Math.Round(weatherBindingData.WeatherDataCurrent.Wind.Speed) * 3.6:0}km/h";
Or what you more likely want:
string wSpeed = $"{weatherBindingData.WeatherDataCurrent.Wind.Speed * 3.6:0}km/h";
This question already has answers here:
How to use NumberFormatInfo to remove parenthesis for negative values
(6 answers)
Closed 5 years ago.
Is there a way to format a decimal so that it appears as a currency with both + or - signs?
For example:
+$5.00 (plus sign for greater than zero)
$0.00 (no sign for zero)
-$5.00 (minus sign for less than zero)
The following does what I want but not sure how to incorporate currency:
var formattedprice = $"{price:+0;-#}"
I would typically use C0 for currency or N0 for number.
class Program
{
static void Main(string[] args)
{
var pos = 5m;
var zero = 0m;
var neg = -5m;
var format = "+$0.00;-$0.00;$0.00";
Console.WriteLine(pos.ToString(format));
Console.WriteLine(zero.ToString(format));
Console.WriteLine(neg.ToString(format));
}
}
and the output is
+$5,00
$0,00
-$5,00
and as per #xxbbcc's comment, currency symbol location depends on locale so you'll have to change the format in accordance.
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 do I format a double to a string and only show decimal digits when necessary?
(2 answers)
Closed 9 years ago.
How to hide fractional part of a double or float number if it is all zero. I am converting a floating point number to string and its display Mantissa part even if it is zero. For example:
double number = 123.00;
string strNumber = number.ToString(); // it shows "123.0", what I need is only "123"
double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString(); // it shows "123.2234" as needed.
Is there any built-in solution in .NET to get it done?
Thanks
Try using the overload of double.ToString() that takes in a format string, and pass it "R":
double number = 123.00;
string strNumber = number.ToString("R");
double secondNumber = 123.2234;
string strSecondNumber = secondNumber.ToString("R");