Correct string formatting (or rounding) - c#

var rounded = Math.Round(value, 1);
string prob = string.Format("{0:P}", rounded);
Example:
value : 0.599..
rounded: 0.6
prob : 60.00 %
I want the prob to be just 60 % How can I do this?

Use {0:0%} if you want "60%" without a space between the number and percent symbol.
Use {0:P0} if you want "60 %" with a space.
var value = 0.599;
var rounded = Math.Round(value, 1);
string prob1 = string.Format("{0:0%}", rounded);
Console.WriteLine(prob1);
// prints "60%"
string prob2 = string.Format("{0:P0}", rounded);
Console.WriteLine(prob2);
// prints "60 %"

Related

C# Convert decimal to string with specify format

I need to convert decimal number a to string b folowing:
b must be haven '.' character. Eg:
a = 12 -> b = "12.0"
a = 1.2 -> b = "1.2"
a = 1.234 -> b = "1.234"
How can I do that with 1 command?
b must be haven exactly 10 character. Eg:
a = 101 -> b = "101.000000"
a = 1.234 -> b = "1.23400000"
a = 1.234567891 -> b = "1.23456789"
(Same question with 1)
decimal a = 12;
var b = a.ToString("N1"); // 12.0
a = 1.2m;
b = a.ToString(); // 1.2
a = 101m;
b = a.ToString("N10"); // 101.0000000000
a = 1.234m;
b = a.ToString("N10"); // 1.2340000000
For the second part of your question - where you want a total length of 10 then:
decimal a = 1.234567891m;
int numberOfDigits = ((int)a).ToString().Length;
var b = a.ToString($"N{9 - numberOfDigits}"); //1.23456789
//Or before C# 6.0
var b = a.ToString("N" + (9 - numberOfDigits)); //1.23456789
Basically ((int)number).ToString().Length gives you the amount of digits before the . (converting to int will remove the fractions) and then reducing that from the number of digits after the . (and also -1 for the decimal point itself)
You can use .ToString() to do this task:
decimal aDecimalVal = 10.234m;
string decimalString = aDecimalVal.ToString("#.000"); // "10.234"
aDecimalVal.ToString("#.00"); // "10.23"
aDecimalVal.ToString("#.0000"); // "10.2340"
The number of 0 after the . in the format string will decide the number of places in the decimal string.
Updates: So you want to find the number of digits after the decimal points, So the changes in the code will be like the following:
decimal aDecimalVal = 10.2343455m;
int count = BitConverter.GetBytes(decimal.GetBits(aDecimalVal)[3])[2];
string formatString = String.Format("N{0}",count.ToString());
string decimalString = aDecimalVal.ToString(formatString); // "10.2343455"
I manage to do it using double. Is this what you need?
I don't quite get the second part of your question.
double a = 12;
string b = a.ToString("0.0000000000######");
Console.WriteLine(b);
if you want the result as a string, just parse it and format it to one decimal places:
string strTemp = 12;
double temp = Double.Parse(strTemp, CultureInfo.InvariantCulture);
string result = temp.ToString("N1", CultureInfo.InvariantCulture);
Round off to 2 decimal places eg. 27.658 => 27.66
Ensure that there are always 2 decimal places eg. 12 => 12.00, 12.5 => 12.50
Good fit for currency amounts.
strTemp.ToString("F");
For the first one, if you don't know how many the digits the variable could be, you can have a extension method:
public static string ToSpecificFormat(this decimal value)
{
var count = BitConverter.GetBytes(decimal.GetBits(value)[3])[2];
return value.ToString(count == 0 ? "N1" : "N" + count);
}
This will make sure there is at least 1 digit in the output.
For the second one, the selected answer will fail if the number > 1000000000. This one should work:
public static string ToFixedLength(this decimal value)
{
if (value >= 1000000000m) return value.ToString("N0");
var format = 9 - Math.Floor(value).ToString().Length;
return value.ToString("N" + format);
}
output:
1.234m.ToFixedLength(); // 1.23400000
101m.ToFixedLength(); // 101.000000
123456789123m.ToFixedLength(); // 123,456,789,123

Format number to string with custom group and decimal separator without changing precision

I would like to format some numbers to strings in C# with custom group/thousands separator and decimal separator. The group and decimal separator can change based on user input so I would like to use a NumberFormatInfo object instead of a hardcoded formatting string. My code below gets the proper separators, but it changes the precision of the number to always be two decimal places, whereas I want to keep the full precision of the number and only show decimal places when needed (so integer values dont have decimal places).
How can I achieve this? I am guessing I need to change the "N" parameter, but change it to what?
double n1 = 1234;
double n2 = 1234.5;
double n3 = 1234567.89;
double n4 = 1234.567;
var nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = " ";
string s1 = n1.ToString("N", nfi); //want "1 234" but I get "1 234,00"
string s2 = n2.ToString("N", nfi); //want "1 234,5" but I get "1 234,50"
string s3 = n3.ToString("N", nfi); //correct output of "1 234 567,89"
string s4 = n4.ToString("N", nfi); //want " 1 234,567" but I get "1 234,57"
Below is the solution I came up with as an extension method.
public static string Format(this double d, NumberFormatInfo numberFormatInfo)
{
string s = d.ToString(CultureInfo.InvariantCulture);
int index = s.IndexOf('.');
int decimalPlaces = index == -1 ? 0 : s.Length - index - 1;
return d.ToString($"N{decimalPlaces}", numberFormatInfo);
}
Edit:
There is a workaround using built-in ToString() (by using digit placeholder #) and Replace and/or Trim:
double n1 = 1234;
double n2 = 1234.5;
double n3 = 1234567.89;
double n4 = 1234.567;
string s1 = n1.ToString("### ### ###.###").Replace(".",",").Trim();
string s2 = n2.ToString("### ### ###.###").Replace(".", ",").Trim();
string s3 = n3.ToString("### ### ###.###").Replace(".", ",").Trim();
string s4 = n4.ToString("### ### ###.###").Replace(".", ",").Trim();
Combining it with numeric format to read , as decimal separator doesn't work though:
double n1 = 1234;
double n2 = 1234.5;
double n3 = 1234567.89;
double n4 = 1234.567;
var nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = " ";
string s1 = n1.ToString("### ### ###,###", nfi); //doesn't work
string s2 = n2.ToString("### ### ###,###", nfi);
string s3 = n3.ToString("### ### ###,###", nfi);
string s4 = n4.ToString("### ### ###,###", nfi);
Original:
I guess you cannot use only the built-in ToString() to get it right. You probably need some LINQ tricks to get the result you want:
double n1 = 1234;
double n2 = 1234.5;
double n3 = 1234567.89;
double n4 = 1234.567;
var nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = " ";
string s1 = new string(n1.ToString("N", nfi).Reverse().SkipWhile(x => x == '0' || x == ',').Reverse().ToArray());
string s2 = new string(n2.ToString("N", nfi).Reverse().SkipWhile(x => x == '0' || x == ',').Reverse().ToArray());
string s3 = new string(n3.ToString("N", nfi).Reverse().SkipWhile(x => x == '0' || x == ',').Reverse().ToArray());
string s4 = new string(n4.ToString("N", nfi).Reverse().SkipWhile(x => x == '0' || x == ',').Reverse().ToArray());
There reason is because of the two things as follow:
First, double is not precise. When you input something like double n = 1234.5, the actual double value stored could be something like n = 1234.499999999999998
Second, it is about ToString(). It is really used only for formatting. In other words, depends on how you dictate it, it will show something out. For instance, if you give instruction to show the number with 2 significant digits after decimal separator, then it will show the number with exactly 2 significant digits after decimal separator.
Now, putting the two things together we got a dilemma here! What you want the program to do is: "Show my as many number of significant digits as it needs to be". However, when you input double n = 1234.5, the program will show 1234.499999999999998 instead! But on the other hand, you also do not want to fix the number after decimal separator.
So, I guess you should be using LINQ SkipWhile and Reverse to do that, not by simple built-in.
//this is the best way for you:
s3 = n3.ToString($"### ##0{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}00");
//or my prefer:
s3 = n3.ToString($"###{CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator}##0{CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator}00");
//I am always using this. nop. it is best and smallest way. :)
//Best Regards. Ersin Kecis.
You can increase the number of decimal places in NumberFormatInfo and use TrimEnd() to remove additional zeros and seperators.
nfi.NumberDecimalDigits = 15;
public static string FormatDouble(double d, NumberFormatInfo nfi)
{
return d.ToString("N", nfi).TrimEnd('0', ' ', ',', '.');
}

double.ToString() return wrong value in C#

I have double variable and its value is :
double d = 0.000000000000056843418860808015;
when i print this variable its print wrong.
d.ToString();
Output : "5.6843418860808E-14"
How to resolve this?
Well if you want to have an output without the exponential notation you need to format your string:
d.toString("F25");
This will give you the "correct" number with up to 25 fractional digits.
0,0000000000000568434188608
Edit: Complete list of formats and conversions are available here and snapshot for you is below.
Original value: 1054.32179
F: 1054.32
F0: 1054
F1: 1054.3
F2: 1054.32
F3: 1054.322
double d = 0.000000000000056843418860808015;
var str = d.ToString("G17");
var value = str.Split('E')[0];
var zeros = Int32.Parse(str.Split('E')[1]);
var outputString = "";
zeros = Math.Abs(zeros);
var addstring = "0.";
for (var i = 0; i < zeros - 1; i++)
{
addstring += '0';
}
value = value.Remove(1, 1);
value = addstring + value;

Converting decimal to 16 character string with or without - sign

I am looking for a method that adds zero's up to 16 characters before a decimal, and a minus sign if the value is minus. E.g.,
18,52 becomes 000000000000001852, and
-18,52 becomes-00000000000001852
I have an idea how to implement this by using replacements and if-statements, and using the PadLeft method where characters are padded to the left to what length you specify. But I am not sure how to make it exactly.
What I have right now is this:
static string FormatDecimal(decimal d, int length = 0, char a = '0')
{
var sb = new StringBuilder();
var rounded = decimal.Round(d, 2, MidpointRounding.AwayFromZero);
if (rounded < 0)
{
sb.Append("-");
}
else
{
sb.Append("");
}
var lastPart = rounded.ToString().Replace(",", "").Replace(".", "");
var lengthMiddle = length - sb.Length - lastPart.Length;
for (int i = 0; i < lengthMiddle; i++)
{
sb.Append(a);
}
sb.Append(lastPart);
return sb.ToString();
}
When I look at the code and do Console.WriteLine(FormatDecimal(-18m, 16, '0')) I see that
The code is 1. very long, and 2. it does not work... The rounding fails and just keeps the -18 and no minus sign is added.
I would be very grateful if someone could help me out with this one!
If you want to represent two decimal digits in your string and eliminate the decimal mark, you can simply multiply your number by 100. To pad up to 16 digits, use the D format string:
decimal d = -18.52m;
string s = ((int)(d * 100)).ToString("D16");
Edit: If you only want to pad up to 15 digits for negative numbers, you could use a conditional:
decimal d = -18.52m;
int i = (int)(d * 100);
string s = i.ToString(i >= 0 ? "D16" : "D15");
Alternatively, you could express the conditional within the format string itself using section separators:
string s = i.ToString("0000000000000000;-000000000000000");
Instead of building this yourself, use what's already there. decimal.ToString() will format a number for you:
decimal d = 18.52;
string s = d.ToString("0000000000.##"); // = "0000000018.52"
decimal d = 18.00;
string s = d.ToString("0000000000.##"); // = "0000000018"
You could build your format string using the string constructor:
int length = 10;
string formatString = string.Concat(new string('0', length), ".##")
string s = d.ToString(formatString); // = "0000000018"
Note: this doesn't take care of your rounding, but I'm not clear from the question what your requirement is there.

how to use the round maths function in asp.net?

I am use the textbox value like 1455.23, use the round function my output is 0000145523 but customer not enter float values like 1234 my output is 0000123400 pls give me suggestion
my code is format.cs
public bool formatAmount(float flAmount, out string strOutput)
{
bool bval = false;
float rounded = (float)Math.Round(flAmount, 2);
if(rounded!=null)
{
flAmount = flAmount * 100;
strOutput = Convert.ToString(flAmount);
bVal = true;
}
return bVal;
}
In my asp page code like this
string ods;
float a = Convert.Todecimal(txtSSA.Text);
string sss = oclsUtility.formatAmount(a, out ods);
I am assuming you want to ignore the multiplication of 100 part in case the fractional value is not there.
So 1234 in your case is essentially 1234.00 and you need to avoid the 0000123400
float flAmount = 15F;
float rounded = (float)Math.Round(flAmount, 2);
double fractionalval = (rounded - Math.Floor(rounded)) * 100;
if(fractionalval > 0)
flAmount = flAmount * 100;
After this i presume rest might work and pad it to 10 length string.
This would skip the multiplication if there are fractional parts, hope this is what you need else please edit for additional information.
If I'm understanding you, you need to keep leading 0 if user input is float and if not removes them. You can try this:
int number;
bool result = Int32.TryParse(rounded, out number);
if (result)
{
// Your number will contain the number with no leading 0
}
else
{
// Then you have a float.
}

Categories

Resources