Format int to string with decimals - c#

I am trying to format a string using string.Format method to produce a fixed decimal string from an int data type.
I have tried following code :
sOutputString = string.Format(
"Days:{0:D1} Hours:{1:D1} Minutes:{2:D1} Seconds:{3:D1} Miliseconds:{4:D1}",
objTimeCalculate.Days,
objTimeCalculate.Hours,
objTimeCalculate.Minutes,
objTimeCalculate.Seconds,
objTimeCalculate.Miliseconds);
The values of the properties Days, Hours, Minutes, Seconds are int data type and formatted using the D format specified. However I need decimal values produced in the string.
Output should be :
sOutputString = Days : double_value Hours : double_value Minutes : double_value Seconds : double_value Miliseconds : integer_value
Since the property days can go beyond reach of the integer datatypes reach.

Given that objTimeCalculate is a TimeSpan all those variables are int. Therefore they dont have a decimal value.
However we could use the format string F1 which will return with a fixed decimal value, however this will always be 0.
Example:
var objTimeCalculate =(DateTime.Now - DateTime.UtcNow);
var sOutputString = string.Format(
"Days:{0:F1} Hours:{1:F1} Minutes:{2:F1} Seconds:{3:F1} Miliseconds:{4:F1}",
objTimeCalculate.Days,
objTimeCalculate.Hours,
objTimeCalculate.Minutes,
objTimeCalculate.Seconds,
objTimeCalculate.Milliseconds);
Result:
Days:0.0 Hours:9.0 Minutes:29.0 Seconds:59.0 Miliseconds:996.0
You can consult Standard Numeric Format Strings for more details.
"F" or "f" Fixed-point
Result: Integral and decimal digits with optional negative sign.
Supported by: All numeric types. Precision specifier: Number of
decimal digits. Default precision specifier: Defined by
NumberFormatInfo.NumberDecimalDigits.
The Fixed-Point ("F") Format Specifier.

You are looking F specifier:
sOutputString = string.Format(
"Days:{0:F2} Hours:{1:F2} Minutes:{2:F2} Seconds:{3:F2} Miliseconds:{4:F2}",
objTimeCalculate.Days,
objTimeCalculate.Hours,
objTimeCalculate.Minutes,
objTimeCalculate.Seconds,
objTimeCalculate.Miliseconds);

Related

Format a number value with minimal decimal points

I need to format a number (decimal) into a string with minimal decimal points.
for example, let's say the minimal decimal point is 3
123.123654 => 123.123654
123.12 => 123.120
123.1 => 123.100
123 => 123.000
What is the best way to achieve this result?
If you use really a decimal the decimal places are preserved, so you can write:
decimal d = 123.120m;
Console.WriteLine(d); // 123.120
If you can't do this you can always provide a format with ToString:
Console.WriteLine(d.ToString("N3"));
Reading: Standard numeric format strings, especially. numeric format specifier
As juharr pointed out this shows just 3 decimal places. You can use string.Format:
string result = string.Format("{0:0.000##################}", d);

Wrong parsing from string to double

double value = double.Parse("4655.927411110702", CultureInfo.InvariantCulture);
Why this parsing is getting me result: 4655.9274111107025?
Somehow it adds to my number 5 at the end. How should I convert this string to double and have a correct result?
You can't. Not all numbers can be represented exactly in double precision floating point, and the closest such double to 4655.927411110702 is 4655.927411110702450969256460666656494140625, and the default formatting you're using trims off the majority of the "joke" digits.
C# does have a decimal type. Can you not use that?
decimal value = decimal.Parse("4655.927411110702", CultureInfo.InvariantCulture);

How to display percentage with one decimal and manage culture with string.Format in C#?

I would like to display a percentage and manage the culture.
Like this : https://msdn.microsoft.com/fr-fr/library/system.globalization.numberformatinfo.percentnegativepattern%28v=vs.110%29.aspx
I do that :
double percentage = 0.239;
NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
string percentageValue = string.Format(nfi, "{0:P1}", percentage);
It works (for example result can be "%23,9" or "23,9 %")
But I don't want to display the decimal if not needed
=> "100 %" instead of "100,0 %".
I try with #.#, its works but I want to manage the current culture (decimal separator, percentage position, etc).
How can I achieve that ?
Thanks !
A period (.) in the format is actually a replaced character: the culture's decimal separator1. See here on MSDN.
So that part is easy.
However the P format's decimal places are based on details in the applicable locale, there is no custom formatting for "percent digits".
Additionally
But I don't want to display the decimal if not neede
is very hard for floating point values. As approximations any attempt at something like if (value.FractionalPart == 0) is doomed the underlying binary representation. For example 0.1 (10%) is not represented exactly and after multiplying by 100 (for the percentage display) is unlikely to be exactly 10. Thus "has no decimal places" will actually need to be "sufficiently close to an integral value":
var hasFraction = Math.Abs(value*100.0 - Math.Round(value*100, 0)) < closeEnough;
and then build the format string depending on the result.
1 Ie. if you want a period independent of culture you need to quote it – with single quotes – eg. value.ToString("#'.'##").
Standard Numeric Format Strings
"P" or "p" (Percent):
Result: Number multiplied by 100 and displayed with a percent symbol.
Supported by: All numeric types.
Precision specifier: Desired number of decimal places.
Default precision specifier: Defined by NumberFormatInfo.PercentDecimalDigits.
More information: The Percent ("P") Format Specifier.
1 ("P", en-US) -> 100.00 %
1 ("P", fr-FR) -> 100,00 %
-0.39678 ("P1", en-US) -> -39.7 %
-0.39678 ("P1", fr-FR) -> -39,7 %
NumberFormatInfo.PercentDecimalDigits contains this example:
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
// Displays a negative value with the default number of decimal digits (2).
Double myInt = 0.1234;
Console.WriteLine( myInt.ToString( "P", nfi ) );
// Displays the same value with four decimal digits.
nfi.PercentDecimalDigits = 4;
Console.WriteLine( myInt.ToString( "P", nfi ) );
Which results in the output:
12.34 %
12.3400 %
Ok thanks, so that's not possible with string.Format()
And what do you think of this ?
bool hasDecimal = !percentage.Value.ToString("P1", CultureInfo.InvariantCulture).EndsWith(".0 %");
string percentageMask = hasDecimal ? "{0:P1}" : "{0:P0}";
string percentageValue = string.Format(CultureInfo.CurrentCulture, percentageMask, percentage);

Formatting a decimal value with ToString() to have commas as thousand separators where the amount of decimal places is unknown

I have many decimals, each rounded differently:
decimal quantity = Decimal.Round(item.Quantity.Value, 2,
MidpointRounding.AwayFromZero);
decimal difference = Decimal.Round(quantity * eva, 0,
MidpointRounding.AwayFromZero);
When binding to the UI, I convert to string like this:
string Quantity = quantity.ToString("G", CultureInfo.InvariantCulture);
string Difference = difference.ToString("G", CultureInfo.InvariantCulture);
Is there a generic way to insert commas for thousand separators while keeping the original decimal rounding the same?
Try using Format.
double d = 1.234567;
String output = d.ToString("#,##0.##");
Also,
double d = 123456789.1;
string format = d.ToString().
IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
>=0 ? "#,##0.00" : "#,##0";
Console.WriteLine (d.ToString(format));
For anyone wondering, I ended up using String.Format(new CultureInfo("en-US"), "{0:N}", difference) and changed the N depending on how many decimal places I needed.
You can use the "N" format specifier and supply the number of digits you want any number to retain. If you want each number to potentially have a different number of digits you wall have to determine the number to supply to the format string each time.
quantity.ToString("N(digits)");
Complete documentation is at http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#NFormatString

Format decimal with positive negative and zero value to 2 decimal places format

I have a parameter price which is of type decimal it can have a positive, negative and zero value. The value will never have a decimal but needs to display a decimal.
While I parse the decimal it does handle 0 and > 0 values but does not handle negative values. How can I handle all 3 in one parse operation?
decimal.Parse(a.price.ToString("##.00"), NumberStyles.AllowDecimalPoint)
I need to convert the data to show in European culture so value of -3.00 should show up as -3,00.
Here is what I am trying and it gives me an exception
var culture = CultureInfo.GetCultureInfo("nl-NL");
Price = a.GroupId == g ? decimal.Parse(a.BidPrice.ToString("##.000", culture), NumberStyles.Number) : decimal.Parse(decimal.Zero.ToString("##.000", culture), NumberStyles.Number)
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
Adding some test data,
3 should show up as 3,00
-3 should show up as -3,00
0 should show up as 0,00
Edit
The default culture is en-GB
Try NumberStyles.Number, that'll work:
decimal price = -3;
decimal result = decimal.Parse(price.ToString("##.00"), NumberStyles.Number);
//Result: -3,00 (comma because I live in Europe :D)
Hope this helps!
Edit:
To ensure a comma use the German CultureInfo as they use a comma for a decimal and the period as the thousand separator:
decimal price = -3;
decimal result = decimal.Parse(price.ToString("##.00", CultureInfo.GetCultureInfo("de-DE")), NumberStyles.Number);

Categories

Resources