0

I'm using drawstring to get a item from a list to be displayed, but now i would like it to be displayed as a 4 digit number instead of a single digit.

So instead of it displaying 0 i would like it to display 0.000 . If anyone could help me with this that would be nice.

This is the list type used.

List<string> ListVrd = new List<string>();

The code used to fill the list.

c = drART["MVRD"].ToString();
ListVrd.Add(c);

And the line of code used to print the item.

row = ListVrd[itemsPrinted];
e.Graphics.DrawString(row, DefaultFont, Brushes.Black, distance, currentY);//print item
maam27
  • 444
  • 3
  • 21

3 Answers3

1

I assume what is coming from drART is a number, in which case you can format it there:

c = ((float)drART["MVRD"]).ToString("0.000");
ListVrd.Add(c);

Note that the above assumes that the number coming from drART["MVRD"] is a float. Check what type it actually is - it may be int,decimal,long etc.

See Standard formatting strings and Custom formatting strings

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • If I try that it gives me this error: No overload for method 'ToString' takes 1 arguments – maam27 Feb 24 '15 at 09:19
  • @maam27 - you need to first cast the result from `drART` to the right type. Ill update answer – Jamiec Feb 24 '15 at 09:20
  • What would I use for numeric? I tried the code with a int but if I go for that it would give me this: "Specified cast is not valid." – maam27 Feb 24 '15 at 09:26
  • Use decimal data type and you can format like this `1001.2511415M.ToString("n3")` – Eric Feb 24 '15 at 09:28
  • @Eric the data inside the database is a integer value, i dont really have any use for a decimal number since it only has full digits. – maam27 Feb 24 '15 at 09:38
  • @maam27 Which do you mean 10 --> 10.00 or 10.000? – Eric Feb 24 '15 at 09:42
  • The field is supposed to show the minimal amount that should be stored of a item. for most of them the minimum is 0 but i want to have it displayed as 0.000 and 0.001 for when it would be 1 etc. – maam27 Feb 24 '15 at 09:49
  • @maam27 - It helps us answer your question if you actually ask the right question and provide the right details. You never specified anything about turning `1` into `0.001` - how were we meant to know that? Also, specifying that the value in the database is an integer saves a whole lot of guesswork. – Jamiec Feb 24 '15 at 10:00
  • What about `string.Format("{0:n3}", 99999 / 1000m)`? Will broke your 4 digit limit if minimal amount > 9999 – Eric Feb 24 '15 at 10:02
0

After trying to make it work using some of the things suggested here and some the msdn list of the format placeholders I managed to get what I wanted.

This is the code im using now.

c = drART["MVRD"].ToString();
c2 = (String.Format("{0:###,###0,000} ", Convert.ToInt32(c), cult));
ListVrd.Add(c2);
maam27
  • 444
  • 3
  • 21
-2

You'll need to convert the string to a numerical type and then convert that back to a string in the desired format, e.g.

formattedString = Convert.ToDouble(unformattedString).ToString("n3");

Where you actually do the conversion is up to you, i.e. before you add the item to the list or when you take it out. I have to wonder though, if the data represents a number then why is it stored as a string in the first place?

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • It's absurd. Instead converting a number to a proper string, when filling the list, you're offering a double conversion. – Dennis Feb 24 '15 at 09:19
  • @Dennis, it's an answer to the question that was asked. There's also no need to use both parts of the conversion. The "double conversion" contains two single conversions so, if you have a number (which I specifically alluded to) then you obviously only have to use the second one. – jmcilhinney Feb 24 '15 at 09:20
  • @jmcilhinney the question was updated shortly after you posted this. – Jamiec Feb 24 '15 at 09:22