This question already has answers here:
Formatting a float to 2 decimal places
(9 answers)
C# How to format a double to one decimal place without rounding
(6 answers)
Closed 11 months ago.
This is what I want to do:
if a double field has more than 3 decimal places then it should convert to 3 decimal figures and if no decimal places are present then it should convert to 3 decimals
e.g 12.878999 -> 12.878
120 -> 120.000
I cannot use string.Format() as I want the double field to stay double.
The first example requires Math.Round, eg Math.Round(d,3,MidPointRounding.ToZero).
The second isn't meaningful. Trailing decimal zeroes aren't significant. In the real types (float, double) they don't affect the storage of the number. The call Math.Round(120d, 3, MidpointRounding.AwayFromZero) will print 120 without a format string.
Displaying a double with three trailing zeroes is a formatting operation.
Update
From the comments it appears the actual problem is how to format a report sum in DevExpress Reports. All report engines allow specifying a format for fields and cells.
The Format Data page in the DevExpress Reports docs shows how to modify the FormatString property for a specific value
You can use Math.Round to achieve it
var value = Math.Round(12.878999, 3, MidpointRounding.ToZero);
For the integer type, you can do it this way
var value = 129 + 0.000m; //extend 3 decimals for an integer number
Related
This question already has answers here:
c# float [] average loses accuracy
(3 answers)
Why is my number being rounded incorrectly?
(5 answers)
Closed 5 years ago.
I'm having some trouble to convert data taken in my database to float numbers. Basically, the application is rounding one of my values to one decimal house. The table contains a set of fields with monetary values and one of these values te application keeps rounding. The strangest thing is that, despite the fact that there are more float values on my database, just one of them is rounded (valorAtualizadoAcordo). So, this is my query:
SELECT
idAcordo,
tipoAcordo,
valorAtualizadoAcordo,
dataAtualizacaoAcordo,
valorConfessadoAcordo,
valorAcordo,
valorEntradaAcordo,
valorParceladoAcordo,
quantidadeParcelasAcordo,
taxaAcordo,
atualizacaoTrAcordo,
dataPrimeiraParcelaAcordo,
dataUltimaParcelaAcordo,
dataAssinaturaAcordo,
dataAprovacaoAcordo,
primeiraDataAbertoAcordo,
dataProtocoloAcordo,
tipoProtocoloAcordo,
cadastroDspsAcordo,
dataSolicitacaoAcordo,
contabilizadoAcordo,
dataLancamentoAcordo,
contratoDevolvidoAcordo,
dataDevolucaoAcordo,
volumetriaAcordo,
pagamentoRealizadoDataAcordo,
contratoAntesAjuizamentoDataAcordo
FROM
acordo
WHERE
FK_idContrato = 46;
And this is my convertion:
ac.setValor(float.Parse(acd.Rows[i][5].ToString(), CultureInfo.InvariantCulture));
The value I'm trying to retrieve from the database is 325,360.69. I'm converting it to PT-BR format, what gives me "325360,7" (in Brazil, we use "," instead of "." to define the decimal floating point).
Not sure why you think making a string from a float and back is a good idea, but I would start by dropping the ToString. The use a cast or a real conversion method like convert.ToFloat:
float f = (float)acd.Rows[i][5];
Or:
float f = Convert.ToFloat(acd.Rows[i][5]);
This question already has answers here:
C# Double - ToString() formatting with two decimal places but no rounding
(15 answers)
Closed 8 years ago.
I want to convert following 3 double values to string.
Double value converted string value
1 1
1.200 1.200
1.666 1.666
I want my output string values in same format as double. Means in above values 1 doesn't have any decimal value, so it will remain same. In case of 1.200 it will give me string as 1.200 and same for 1.666. However I tried .ToString() method but it truncated the ZERO value for 1.200 value. But i don't want this. I want Actual values in string format.
I admit that I don't like the idea of doing this, but I wrote an extension method for the Double type to format it. Feel free to change the funny names. :)
public static class StupidDouble
{
public static string StupidFormat(this double theValue)
{
// geth the double value to three decimal points
string s = string.Format("{0:0.000}", theValue);
// get the whole number value
int check = (int) Math.Truncate(theValue);
// if the format of the int matches the double, display without decimal places
if (string.Format("{0:0.000}", check) == s)
{
s = check.ToString();
}
return s;
}
}
C#, or any other language (including SQL), does not make differences in the values of floating-point types (float, double) based on their representation in your code.
This means that the compiler does not make a difference between:
double v1 = 1;
and
double v2 = 1.000;
Neither does the database where you say (in the comments) you get these values from.
The reason databases display decimals in a certain way is because of a pre-defined formatting applied to the result set. This could either be defined by the database tool you use, or depend on your locale system settings. Anyway, the .NET framework provides you with capabilities to explicitly format your data.
You need to decide which formatting suits your needs best an use it. For instance this will format the number with 4 decimal places after the dot:
String.Format("{0:0.0000}", v1); // outputs 1.0000
String.Format("{0:0.0000}", v2); // outputs 1.0000 too
If you already know your desired format, adjust the formatting string (the first argument that looks like "{0:0000}" in a way that best corresponds to your requirements.
This question already has answers here:
Formatting doubles for output in C#
(10 answers)
Closed 8 years ago.
Is there a way to get a string showing the exact value of a double, with all the decimal places needed to represent its precise value in base 10?
For example (via Jon Skeet and Tony the Pony), when you type
double d = 0.3;
the actual value of d is exactly
0.299999999999999988897769753748434595763683319091796875
Every binary floating-point value (ignoring things like infinity and NaN) will resolve to a terminating decimal value. So with enough digits of precision in the output (55 in this case), you can always take whatever is in a double and show its exact value in decimal. And being able to show people the exact value would be really useful when there's a need to explain the oddities of floating-point arithmetic. But is there a way to do this in C#?
I've tried all of the standard numeric format strings, both with and without precision specified, and nothing gives the exact value. A few highlights:
d.ToString("n55") outputs 0.3 followed by 54 zeroes -- it does its usual "round to what you probably want to see" and then tacks more zeroes on the end. Same thing if I use a custom format string of 0.00000...
d.ToString("r") gives you a value with enough precision that if you parse it you'll get the same bit pattern you started with -- but in this case it just outputs 0.3. (This would be more useful if I was dealing with the result of a calculation, rather than a constant.)
d.ToString("e55") outputs 2.9999999999999999000000000000000000000000000000000000000e-001 -- some of the precision, but not all of it like I'm looking for.
Is there some format string I missed, or some other library function, or NuGet package or other library, that is able to convert a double to a string with full precision?
You could try using # placeholders if you want to suppress trailing zeroes, and avoid scientific notation. Though you'll need a lot of them for very small numbers, e.g.:
Console.WriteLine(double.Epsilon.ToString("0.########....###"));
I believe you can do this, based on what you want to accomplish with the display:
Consider this:
Double myDouble = 10/3;
myDouble.ToString("G17");
Your output will be:
3.3333333333333335
See this link for why: http://msdn.microsoft.com/en-us/library/kfsatb94(v=vs.110).aspx
By default, the return value only contains 15 digits of precision although a maximum of 17 digits is maintained internally. If the value of this instance has greater than 15 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format with the "G17" format specification, which always returns 17 digits of precision, or "R", which returns 15 digits if the number can be represented with that precision or 17 digits if the number can only be represented with maximum precision.
You can also do:
myDouble.ToString("n16");
That will discard the 16th and 17th noise digits, and return the following:
3.3333333333333300
If you're looking to display the actual variable value as a number, you'll likely want to use "G17". If you're trying to display a numerical value being used in a calculation with high precision, you'll want to use "n16".
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
i have value of 1.564 and 1.565 i need round this value to 1.56 and 1.56,which function suitable for this in c#.
do a multiply by 100 followed by a floor and followed by a divide by hundred. I am sure that there is a better way of doing it though
Math.floor(n*100)/100
To remove the less significant digits (1.348 -> 1.34):
Math.Floor(number * 100) / 100;
To round the number to two decimals:
Math.Round(number, 2);
To represent it as a string, for display:
number.ToString("#.00");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)
How to round decimal value up to nearest 0.05 value??, the linked SO post also discusses the similar topic, but its not the output i expected.
I need to convert the decimal values like this
16.489-->16.49
16.482-->16.48
16.425-->16.43
7.67 --> 7.67 (no conversion)
I can use the below C# method to convert the values
Math.Round(16.482*20)/20;
But this method not works for me, it gives the following results
16.489-->16.5
16.482-->16.5
7.67 --> 7.7
16.425-->16.45
whats the elegant way in c# to do this.
Math..::.Round Method (Decimal, Int32, MidpointRounding)
Rounds a double-precision floating-point value to the specified number of fractional digits. A parameter specifies how to round the value if it is midway between two other numbers.
Math.Round(1.489,2,MidpointRounding.AwayFromZero)
Did you try
Math.Round(16.482*200)/200;