-1

Hi I can't find any good way to format this string to a decimal.

3942.000000000000

Any ideas on it?

EDIT
How is this now a valid question? Some (as I) might miss the CultureInfo that's suppose to go in the decimal.Parse, for just different culture formats.

Also I didn't want a float, so making a float to decimal is extra steps. Just my two cents

Dejan.S
  • 18,571
  • 22
  • 69
  • 112
  • Do you mean *parse*? Formatting converts from a value to a string, not the other way round. Look at `decimal.Parse`. – Jon Skeet Jan 23 '14 at 13:31
  • 2
    How should the output differ from the decimal conversion of `3942`? Or, how does the standard approach fail? – John Dvorak Jan 23 '14 at 13:31
  • possible duplicate of [Parse string to float number C#](http://stackoverflow.com/questions/5365566/parse-string-to-float-number-c-sharp) – John Dvorak Jan 23 '14 at 13:33
  • Must be missing something. Seems to simple to be true. – SPArcheon Jan 23 '14 at 13:37
  • @SPArchaeologist tried it? – Dejan.S Jan 23 '14 at 13:41
  • @JanDvorak well the number can be 3333.23459876545, `decimal.parse` can't handle that. Except when doing the `CultureInfo.InvariantCulture` – Dejan.S Jan 23 '14 at 13:42
  • It's still a valid question. Missing the `CultureInfo.InvariantCulture`and it wont work. – Dejan.S Jan 23 '14 at 13:44
  • @Dejan.S - Actually, I was meaning that I fear you have some other problem that prevents you from using decimal.Parse as Jon correctly suggested - that would be the first thing that comes up on a google search, and it seemed strange you missed that (2k user, 5 gold, pretty trustworthy I would say). Sorry to have seemed enigmatic, what I meant was "Do you have something that prevents using standard .Parse methods?" – SPArcheon Jan 23 '14 at 13:47
  • Now I see that it was just a problem related to the decimal separator. Didn't realize you may just be missing that step (living in a country where the decimal separator is "," and working daily with code that uses ".", I tend to give culture conversions as given. Again, sorry for the confusion ^_^). – SPArcheon Jan 23 '14 at 13:51

4 Answers4

6

Have you tried Decimal.Parse with specifying decimal separator?

var yourString = "3942.000000000000";
var info = new NumberFormatInfo { NumberDecimalSeparator = "." };
var parsed = Decimal.Parse(yourString, info);

EDIT: as Jon suggested you can use InvariantCulture:

var yourString = "3942.000000000000";
var parsed = Decimal.Parse(yourString, CultureInfo.InvariantCulture);
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
4

Any reason you cannot use

Convert.ToDecimal("3942.000000000000");
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
ElectricRouge
  • 1,239
  • 3
  • 22
  • 33
0

You can use TryParse on the string. Please note the out in the call.

string yourString = "3942.0000000000"
decimal yourDecimal;
decimal.TryParse(yourString, out yourDecimal)
KSdev
  • 621
  • 3
  • 12
0

You can use TryParse:

string yourString = "3942.0000000000"

decimal d;
if (decimal.TryParse(yourString, out d))
{
     //OK
}
else
{
     // Handle failure
}
Peter
  • 27,590
  • 8
  • 64
  • 84
  • Was the absence of a closing parenthesis intentional or accidental? :) – user Jan 23 '14 at 13:35
  • I only did it in my head. Didn't want to go in and edit your answer just in case it was actually intentional. – user Jan 23 '14 at 13:40