# C# gurus:  Help with String.Format() method.

**URL:** https://boards.straightdope.com/t/c-gurus-help-with-string-format-method/464470
**Category:** Factual Questions
**Created:** [September 20, 2008, 7:21pm UTC](https://boards.straightdope.com/t/c-gurus-help-with-string-format-method/464470 "2008-09-20T19:21:10Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [September 20, 2008, 7:21pm UTC](https://boards.straightdope.com/t/c-gurus-help-with-string-format-method/464470/1 "2008-09-20T19:21:10Z")

</div>

Ok, I’ve got a form with a number of textboxes, and I’m trying to populate them with data in the form “123.0” using the following code snippet:

```auto

double V_In = Convert.ToDouble(OutputVNom.Text);
string value = Convert.ToString(V_In * 1.1);
VinHigh.Text = String.Format("{0:0.0}", value);

```

This is supposed to take text data from one textbox, do some math on it, output the result to another textbox and pad the value with a trailing 0 in the event it’s a round number. It calculates the value correctly and if the result has a remainder, it properly formats it with one decimal place (123.789 formats as 123.8), but if it’s an integer result, it doesn’t work (123.00 formats as 123, not 123.0). What am I doing wrong here? My understanding of the String.Format() method from [here](http://blog.stevex.net/index.php/string-formatting-in-csharp/) and other resources suggests this should work the way I want, but it ain’t. This is .NET C# under Visual Studio 2005.

---

<div class="post-metadata">

### Author: ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)
#### Post date: [September 20, 2008, 8:19pm UTC](https://boards.straightdope.com/t/c-gurus-help-with-string-format-method/464470/2 "2008-09-20T20:19:16Z")

</div>

You might try it with hash marks. Either way it looks borked up.

My suggestion if all else fails is to test for the presence of any decimal values and if not add 0.0000000001

---

<div class="post-metadata">

### Author: ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)
#### Post date: [September 20, 2008, 8:27pm UTC](https://boards.straightdope.com/t/c-gurus-help-with-string-format-method/464470/3 "2008-09-20T20:27:58Z")

</div>

Hm, looking at your code, I’m not sure what the second line is meant to be accomplishing? Why are you converting to a string, just to then format your temporary string as a number?

```auto

double V_In = Convert.ToDouble(OutputVNom.Text);
V_In *= 1.1;
VinHigh.Text = V_In.ToString("{0:0.0}");

```

^ Appears to be the preferred method. Certainly it means that you won’t be losing your double’s type when you go to format. But I admit to not knowing C#.

---

<div class="post-metadata">

### Author: ![Q.E.D](https://avatars.discourse-cdn.com/v4/letter/q/51bf81/32.png) [@Q.E.D](https://boards.straightdope.com/u/Q.E.D)
#### Post date: [September 20, 2008, 8:39pm UTC](https://boards.straightdope.com/t/c-gurus-help-with-string-format-method/464470/4 "2008-09-20T20:39:47Z")

</div>

> [@Sage\_Rat](#):
>
> ```auto
> 
> double V_In = Convert.ToDouble(OutputVNom.Text);
> V_In *= 1.1;
> VinHigh.Text = V_In.ToString("{0:0.0}");
> 
> ```

This turned out to be close. To format in the ToString() method, you don’t use (or, indeed, need) an index value, so I used:

```auto

V_In *= 1.1;
VinHigh.Text = V_In.ToString("0.0");

```

Which works perfectly. Thanks!

---

<div class="post-metadata">

### Author: ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)
#### Post date: [September 20, 2008, 8:49pm UTC](https://boards.straightdope.com/t/c-gurus-help-with-string-format-method/464470/5 "2008-09-20T20:49:19Z")

</div>

Then yeah, your problem was that you were trying to format a non-numeric (a string) as a numeric. In making it’s best guess at how to do that, it probably got confused and forgot to do some of the formatting.
