I am having trouble with rounding up to 2 decimal places [duplicate] - c#

In C# I want a function that rounds a given double to a given amount of decimals. I always want my function to return a value (which can be a string) with the given amount of decimals. If necessary, trailing zeros need to be added.
Example:
string result = MyRoundingFunction(1.01234567, 3);
// this must return "1.012"
That's easy, it's just rounding and converting to string. But here comes the problem:
string result2 = MyRoundingFuntion(1.01, 3);
// this must return "1.010"
Is there a convenient/standard way to do this, or do I manually need to add trailing zeros?
Any help is appreciated. Note that in the real life application I can't hard code the number of decimals.

You can create a formatter like this example:
int numDigitsAfterPoint = 5;
double num = 1.25d;
string result = num.ToString("0." + new string('0', numDigitsAfterPoint));
or (more easily)
string result = num.ToString("F" + numDigitsAfterPoint);
As a sidenote, ToString uses the MidpointRounding.AwayFromZero instead of the MidpointRounding.ToEven (also called Banker's Rounding). As an example:
var x1 = 1.25.ToString("F1");
var x2 = 1.35.ToString("F1");
var x3 = Math.Round(1.25, 1).ToString();
var x4 = Math.Round(1.35, 1).ToString();
These will produce different result (because Math.Round normally uses MidpointRounding.ToEven)
And note that internally ToString() seems to do some "magic" before rounding digits. For doubles, if you ask him less than 15 digits, I think it rounds to 15 digits first and then rounds to the right number of digits. See here https://ideone.com/ZBEis9

You should first round, then format.
String.Format("{0:0.000}", Math.Round(someValue, 2));
What you should read is:
Math.Round
String.Format, Custom Numeric Format
As option you could use the extension to support that
Extension Methods

string.format("{0:f2}", value);

Your solution (does what you want).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string result = MyRoundingFunction(1.01234567, 3);
Console.WriteLine(result);
result = MyRoundingFunction(1.01, 3);
Console.WriteLine(result);
Console.ReadLine();
}
public static string MyRoundingFunction(double value, int decimalPlaces)
{
string formatter = "{0:f" + decimalPlaces + "}";
return string.Format(formatter, value);
}
}
}

Simply:
decimal.Round(mydouble, 2).ToString("0.00")

Related

How to get the next decimal given a decimal/precision

I want to determine the next decimal after a certain decimal programmatically.
For example, I have the decimal 0.0000041
I want to programmatically generate 0.0000042 in a function.
If I ran the function again after running it with 0.0000042 the new number would be 0.0000043
If I ran the function with the decimal being 0.0000049 the new number would be 0.0000050 etc....
Is there a way to do this?
I do not know where to start or what references to look at to do something like this.
Thank you.
You seem to be looking for something like this: (Check below if you want to specify the precision manually)
static decimal GetNextDecimal(decimal input)
{
int count = BitConverter.GetBytes(decimal.GetBits(input)[3])[2];
return input + (1m / (decimal)Math.Pow(10, count));
}
Usage:
decimal num1 = 0.0000041m;
decimal num2 = 0.00002m;
decimal next = GetNextDecimal(num1);
decimal next2 = GetNextDecimal(num2);
Console.WriteLine(next);
Console.WriteLine(next2);
Output:
0.0000042
0.00003
I used a little help of this answer to get the number of the decimal places.
If you originally wanted to specify the precision manually, you can use the following instead:
static decimal GetNextDecimal(decimal input, int precision)
{
return input + (1m / (decimal)Math.Pow(10, precision));
}
..which allows you to do something like this:
decimal num1 = 0.003m;
decimal next = GetNextDecimal(num1, 3);
decimal next2 = GetNextDecimal(num1, 4);
Console.WriteLine(next);
Console.WriteLine(next2);
Output:
0.004
0.0031

Format Double to fixed Width C#

My requirements are as follows:
I have a double number. It can be positive as well as negative. I want to use String.Format() to format it.
The Output Number(positive(no sign)/negative(including sign)/zero) should be fixed width: 11
All numbers should have 2 places of decimal.
Examples:
1.2 00000001.20
.2 00000000.20
-.2 -0000000.20
12 00000012.00
0 00000000.00
-0 00000000.00
12.555 00000012.55
Basically, what I want is fixed width(11 digits). 2 Decimal points. No positive sign. Negative sign included in fixed width.
What I have tried:
{0:00000000.00;0:0000000.00;0:00000000.00}
Please tell me what is the right way to do this.
You can use section modifiers to give the output you need:
string format = "{0:00000000.00;-0000000.00}";
Console.WriteLine(string.Format(format, 1.2));
Console.WriteLine(string.Format(format, .2));
Console.WriteLine(string.Format(format, -.2));
Console.WriteLine(string.Format(format, 12.0));
Console.WriteLine(string.Format(format, -0.0));
Console.WriteLine(string.Format(format, 12.555));
Prints out:
00000001.20
00000000.20
-0000000.20
00000012.00
00000000.00
00000012.56
It's basically {position:postive format;negative format;zero format}
Note: position is only used for formatted strings, you can also use it with double.ToString(string) without the "position" element;
If zero format is not provided, format for positive format section is used to format Zero.
See https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx for more detail about the ; section format.
string.Format("{0:00000000.00}", 1.2)
works fine for me
You'll need a second format section for negative numbers. Declaring it as a Func or extension method might be helpful if you need to reuse it. Like so:
using System;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Func<double, string> toFixedWidth = (d) => {
return string.Format("{0:00000000.00;-0000000.00}", d);
};
foreach (var d in new double[] { 1.2, 0.2, -0.2, 12, 0, -0, 12.555 })
{
Console.WriteLine(toFixedWidth(d));
}
}
}
}
00000001,20
00000000,20
-0000000,20
00000012,00
00000000,00
00000000,00
00000012,56
Press any key to continue . . .
double d = -12.365;
string s = d.ToString("0:00000000.00"); //-0:00000012.37
string sOut = d.ToString("00000000.00"); //-00000012.37
Here's a general function for this. It works the same as the other answers, but it builds the formatting string dynamically based on the length (total length) and decimals parameters.
public static string FormatDoubleAsFixedLengthString(double value, int length, int decimals)
{
int wholeDigits = length - decimals;
// Provide a spot for the negative sign if present.
if (value < 0)
{
wholeDigits--;
}
string wholeDigitsFormat = new String('0', wholeDigits);
string decimalDigitsFormat = new String('0', decimals);
string format = $"{{0:{wholeDigitsFormat}.{decimalDigitsFormat}}}";
return string.Format(format, value);
}
And while we're at it, here's the same function but for integers:
public static string FormatIntAsFixedLengthString(int value, int length)
{
// Provide a spot for the negative sign if present.
if (value < 0)
{
length--;
}
return string.Format($"{{0:D{length}}}", value);
}

Count Zeroes before the Decimal Point

I am trying to count how many zeroes are a before a decimal.
private void textBox1_TextChanged(object sender, EventArgs e)
{
decimal x = 0;
if (Decimal.TryParse(textBox1.Text, out x))
{
var y = 1000000;
var answer = x * y;
displayLabel2.Text = (x.ToString().Replace(".", "").TrimStart(new Char[] { '0' }) + "00").Substring(0, 2);
}
else
{
displayLabel2.Text = "error";
}
}
When I plug in (lets say) 7.2 I get an output that displays 72, which is what I want. Now I need another display. That initial 7.2 is being multiplied by 1000000. So the quotent of that would be 7,200,000.00. Now I need to some how count the 5 zeroes before the decimal point and display 5 for that. Then if I were to do .72. My Quotent would be 720,000.00. And I would need to display 4, for the 4 zeroes. And so on. Then I need to output that number to displayLabel5.Text
Here's a one line Linq you could try to count zeroes before the decimal. You can Split() first by the decimal then perform a Where().Count() to get the number of zeros.
using System;
using System.Linq;
public class Program
{
public static void Main()
{
string myString = (720000.00).ToString();
Console.WriteLine(myString.Split('.')[0].Where(d => d == '0').Count());
}
}
Results:
4
Demo
Quick and dirty code so be careful, but AFAIK this is the fastest way to do it.
// Input assuming you've sanitised it
string myInputString = "720000.00";
// Remove the decimals
myInputString = myInputString.Substring(0, myInputString.IndexOf("."));
// The count
int count = 0;
// Loop through and count occurrences
foreach (char c in myInputString)
{
if (c == "0")
{
count++;
}
}
Count is now 4.
Guarantee you this is faster than Regex ;-)
Edit: Sorry for the multiple edits, it's been a long day. Need coffee.
use a regular expression to find all the zeros before the period, then get the string length of that match.
Regex regex = new Regex(#"(0+)\.?");
string value1 = "7,200,000.00";
value1 = value1.Replace(",",""); //get rid of the commas
Match match = regex.Match(value1);
if (match.Success)
{
Console.WriteLine(match.Value.Length);
}
As always test the code because I wrote it just now here in this little text box and not in actual visual studio where I could compile and test it myself. But this should at least illustrate the methodology.
Edit:
slight tweak to the regex to account for the possibility that the number will not display a decimal point at all.

Getting Exact precision of decimal places from a double in .NET 3.5

Due to a requirement I need very exact precision of a double value to a 4 decimal places as follows:
double myDoubleValue = 50234.9489898997952932;
From the above, I need the output as 50234.9489. I DON'T want Rounding the number in this requirement.
I came across with "Math.Truncate(a * 100) / 100;". But really i'm not interested with this approach.
I'm looking for better approach very simple way something like using String.Format or with Regular Expressions etc.
double d = 50234.94895345345345;
var Expected_result = Double.Parse((Regex.Match(d.ToString(), "[+-]?\\d*.\\d{0,4}")).Value);
You would need to do this yourself. One of the possible solutions would be to use an extension method
public static class DoubleEx
{
public static double TruncateFraction(this double value, int fractionRound)
{
double factor = Math.Pow(10, fractionRound);
return Math.Truncate(value * factor) / factor;
}
}
And this is how to use it
double foo = 50234.9489898997952932;
double bar = foo.TruncateFraction(4);
Console.WriteLine(foo); //50234.9489898997952932
Console.WriteLine(bar); //50234.9489
Without regexes:
This works very much fine for any double combinations
using System.Globalization;
class Program
{
static void Main(string[] args)
{
double d = 50234.9489898997952932;
char probablyDot = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
string[] number = d.ToString().Split(probablyDot);
//Console.WriteLine(number[0] + probablyDot + number[1].Remove(4));
Console.WriteLine(number[0] + probablyDot + (number.Length >1 ? (number[1].Length>4? number[1].Substring(0,4):number[1]): "0000"));
//Output: 50234.9489
Console.ReadKey();
}
}
There are a lot of answers here that work with the input given in the question, but on testing them with a range of values, they all have limitations (see comments).
The only way I can see to achieve this with any decimal input is the following. It might not be a one liner, but it seems robust to me.
private static string TrimDecimalPlaces(double value, int numberOfDecimalPlaces)
{
string valueString = value.ToString();
if (!valueString.Contains(".")) return valueString;
int indexOfDot = valueString.IndexOf(".");
if ((indexOfDot + numberOfDecimalPlaces + 1) < valueString.Length)
{
return valueString.Remove(indexOfDot + numberOfDecimalPlaces + 1);
}
return valueString;
}
I've tested this with the following test data and the results are as expected:
1
1.1
1.11
1.111
1.1111
1.11111
1.111111
-1
-1.1
-1.11
-1.111
-1.1111
-1.11111
-1.111111

How to round double values but keep trailing zeros

In C# I want a function that rounds a given double to a given amount of decimals. I always want my function to return a value (which can be a string) with the given amount of decimals. If necessary, trailing zeros need to be added.
Example:
string result = MyRoundingFunction(1.01234567, 3);
// this must return "1.012"
That's easy, it's just rounding and converting to string. But here comes the problem:
string result2 = MyRoundingFuntion(1.01, 3);
// this must return "1.010"
Is there a convenient/standard way to do this, or do I manually need to add trailing zeros?
Any help is appreciated. Note that in the real life application I can't hard code the number of decimals.
You can create a formatter like this example:
int numDigitsAfterPoint = 5;
double num = 1.25d;
string result = num.ToString("0." + new string('0', numDigitsAfterPoint));
or (more easily)
string result = num.ToString("F" + numDigitsAfterPoint);
As a sidenote, ToString uses the MidpointRounding.AwayFromZero instead of the MidpointRounding.ToEven (also called Banker's Rounding). As an example:
var x1 = 1.25.ToString("F1");
var x2 = 1.35.ToString("F1");
var x3 = Math.Round(1.25, 1).ToString();
var x4 = Math.Round(1.35, 1).ToString();
These will produce different result (because Math.Round normally uses MidpointRounding.ToEven)
And note that internally ToString() seems to do some "magic" before rounding digits. For doubles, if you ask him less than 15 digits, I think it rounds to 15 digits first and then rounds to the right number of digits. See here https://ideone.com/ZBEis9
You should first round, then format.
String.Format("{0:0.000}", Math.Round(someValue, 2));
What you should read is:
Math.Round
String.Format, Custom Numeric Format
As option you could use the extension to support that
Extension Methods
string.format("{0:f2}", value);
Your solution (does what you want).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string result = MyRoundingFunction(1.01234567, 3);
Console.WriteLine(result);
result = MyRoundingFunction(1.01, 3);
Console.WriteLine(result);
Console.ReadLine();
}
public static string MyRoundingFunction(double value, int decimalPlaces)
{
string formatter = "{0:f" + decimalPlaces + "}";
return string.Format(formatter, value);
}
}
}
Simply:
decimal.Round(mydouble, 2).ToString("0.00")

Categories