I have below digits. I want to show one digit after to decimal. How to format it?
2.85
2
1.99
I was using ("{0:0.0}". But data showing like
2.9 //It should be 2.8
2.0 //It should be 2
2.0 //It should be 1.9
Try using "{0:0.#}" as the format string. However, that will only fix the .0. To fix the rounding to always round down, you might want to use:
string s = (Math.Floor(value * 10) / 10).ToString("0.#");
Decimal[] decimals = { new Decimal(2.85), new Decimal(2), new Decimal(1.99) };
foreach (var x in decimals)
{
Console.WriteLine(string.Format("{0:0.#}", Decimal.Truncate(x * 10) / 10));
}
// output
2.8
2
1.9
Related
I want to display a float as a string while making sure to display at least one decimal place. If there are more decimals I would like those displayed.
For example:
1 should be displayed as 1.0
1.2345 should display as 1.2345
Can someone help me with the format string?
Use ToString(".0###########") with as much # as decimals you want.
This solution is similar to what other are saying, but I prefer to use string.Format.
For example:
float myFloat1 = 1.4646573654;
float myFloat2 = 5;
Console.WriteLine(string.Format("Number 1 : {0:0.00##}", myFloat1));
Console.WriteLine(string.Format("Number 2 : {0:0.00##}", myFloat2));
// Newer Syntax
Console.WriteLine($"{myFloat1:0.00##}";
Console.WriteLine($"{myFloat2:0.00##}";
This would produce :
Number 1 : 1.4646
Number 2 : 5.00
Number 1 : 1.4646
Number 2 : 5.00
Try this:
doubleNumber.ToString("0.0###");
And, for your reference (double ToString method): http://msdn.microsoft.com/en-us/library/kfsatb94.aspx
float fNumber = 1.2345; // Your number
string sNumber = fNumber.ToString(); // Convert it to a string
If ((sNumber.Contains(".") == false) && (sNumber.Contains(",") == false)) // Check if it's got a point or a comma in it...
{
sNumber += ".0"; // ... and if not, it's an integer, so we'll add it ourselves.
}
say I have these 3 floats:
10,
10.12
10.234
And I want to print at most 1 decimal, but no decimals if not needed, like this:
10
10.1
10.2
You are looking for "#.#" format in ToString. Use it like:
decimal d = 10M;
string str = d.ToString("#.#", CultureInfo.InvariantCulture);
Consider the following example:
List<float> list = new List<float> { 10f, 10.12f, 10.234f };
foreach (var item in list)
{
Console.WriteLine("{0} => {1}", item, item.ToString("#.#", CultureInfo.InvariantCulture));
}
and you will get:
10 => 10
10.12 => 10.1
10.234 => 10.2
See: Custom Numeric Format Strings - MSDN
For the comment:
With the provided example works fine, However when used on 0.2 it
returns .2. Is this the only way? –
Use: "0.#,
If you look at the above specified link then for 0 you will find:
Replaces the zero with the corresponding digit if one is present;
otherwise, zero appears in the result string.
am using Ms chart and i just need a number or numeric value for percentage calculation so for that am using #PERCENT{N} but its returning a decimal value like 0.45 0.78 0.11 etc.. which i do not need, i want it should give me like only numeric value like 45 78 11 23 etc.. please see my below code. its just a part of my coding am posting here. please help me out.
LegendCellColumn PercentageColumn = new LegendCellColumn();
PercentageColumn.Text = "#PERCENT{N}";
PercentageColumn.HeaderText = "%";
PercentageColumn.Name = "Percentage %";
//avgColumn.HeaderBackColor = Color.WhiteSmoke;
Chart1.Legends[0].CellColumns.Add(PercentageColumn);
What should i make with this to get only numeric value with out decimal value.
Thanks
Decimal outDec;
if (Decimal.TryParse("<some string>", out outDec))
{
// Sets Decimal as a Percentage out of 100
PercentageColumn.Text = Convert.ToInt32(outDec * 100).ToString();
}
Edit
To do it specifically using String formatting :
PercentageColumn.Text = "#PERCENT{P0}";
From: Social.MSDN - Shows Percentages on Pie Chart ( Aspnet)
and
MSDN - Standard Numeric Format Strings
I want to add comma to decimal numbers every 3 digits using c#.
I wrote this code :
double a = 0;
a = 1.5;
Interaction.MsgBox(string.Format("{0:#,###0}", a));
But it returns 2.
Where am I wrong ?
Please describe how can I fix it ?
double a = 1.5;
Interaction.MsgBox(string.Format("{0:#,###0.#}", a));
Here is how to do it:
string.Format("{0:0,0.0}", a)
There is a standard format string that will separate thousand units: N
float value = 1234.512;
value.ToString("N"); // 1,234.512
String.Format("N2", value); // 1,234.51
Its doing it right. #,##0 means write at least one digit and zero decimals and space digit groups with comas. Therefore it rounds 1.5 to 2 as it cant write decimals. Try #,##0.00 instead. You'll get 1.50
Try the following format:
string.Format("{0:#,0.0}", a)
Did you tried by this:-
string.Format("{0:0,000.0}", 1.5);
I have a string which needs a decimal place inserted to give a precision of 2.
3000 => 30.00
300 => 3.00
30 => .30
Given a string input, convert to integer, divide by 100.0 and use String.Format() to make it display two decimal places.
String.Format("{0,0:N2}", Int32.Parse(input) / 100.0)
Smarter and without converting back and forth - pad the string with zeros to at least two characters and then insert a point two characters from the right.
String paddedInput = input.PadLeft(2, '0')
padedInput.Insert(paddedInput.Length - 2, ".")
Pad to a length of three to get a leading zero. Pad to precision + 1 in the extension metheod to get a leading zero.
And as an extension method, just for kicks.
public static class StringExtension
{
public static String InsertDecimal(this String #this, Int32 precision)
{
String padded = #this.PadLeft(precision, '0');
return padded.Insert(padded.Length - precision, ".");
}
}
// Usage
"3000".InsertDecimal(2);
Note: PadLeft() is correct.
PadLeft() '3' => '03' => '.03'
PadRight() '3' => '30' => '.30'
Use tryParse to avoid exceptions.
int val;
if (int.Parse(input, out val)) {
String.Format("{0,0:N2}", val / 100.0);
}
here's very easy way and work well..
urValue.Tostring("F2")
let say..
int/double/decimal urValue = 100;
urValue.Tostring("F2");
result will be "100.00"
so F2 is how many decimal place u want
if you want 4 place, then use F4