0

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.

5
  • 3
    Please include your expected output. Commented Aug 17, 2016 at 12:59
  • As @Nico has stated. Please give an example of the type of data going in and what you ultimately expect sOutputString to look like. Commented Aug 17, 2016 at 13:03
  • Use F or G format. Commented Aug 17, 2016 at 13:05
  • Can you explain me how to user F or G? @Paweł Dyl Commented Aug 17, 2016 at 13:08
  • See different formats here: msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx Commented Aug 17, 2016 at 13:09

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The D specifier is only for int types and does not have an exponential value. You should stick with Fif you must show the decimal value. Trying to use D on anything else than an int will result in a Format Exception.
If I use F I get accurate output but output doesn't look good for a very large number
If you review the standard format strings you will see there are many other options. If you are looking to add comma separators for large numbers use the N format provider.
0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.