cagin
cagin

Reputation: 5930

How do I convert a string to a decimal, and format it for pretty output?

I want to convert "372551.40" to decimal. But I need to see it after converting this format 372.551,40.

Upvotes: 1

Views: 2411

Answers (10)

Alex Mendez
Alex Mendez

Reputation: 5150

To convert it to decimal, you can use:

decimal decimalValue = 0.0;

decimalValue = decimal.Parse("372551.40");

or

decimal.TryParse("372551.40", out decimalValue);

To display it in a specific format you can do:

CultureInfo tr = new CultureInfo("tr-TR");    
string formattedValue = decimalValue.ToString("c", tr);
//result will be 372.551,40 YTL

formattedValue = decimalValue.ToString("0,0.00", tr);
//result will be 372.551,40

Upvotes: 2

Scott Munro
Scott Munro

Reputation: 13606

The overload of decimal.Parse that takes an IFormatProvider will allow you to parse strings containing numbers with periods as decimal point symbols (in case the standard is a comma in your culture).

You can use ToString on the resulting decimal to format it with a comma by passing in an appropriate IFormatProvider. Both CulturInfo and NumberFormatInfo implement IFormatProvider.

You can get an instance of CultureInfo with the following code (this one is for English in Australia).

new CultureInfo("en-AU")

Also note that decimal.TryParse is a good alternative to the decimal.Parse method if you expect incorrectly formatted strings as it will allow you to handle them without an exception being raised.

The following code should give you the desired result (you wrote in one of the comments that the target system is SAP and that the culture is probably German (de-DE)).

var yourString = "372551.40";
var yourDecimal = decimal.Parse(yourString, CultureInfo.InvariantCulture);
var yourFormattedDecimal = yourDecimal.ToString(new CultureInfo("de-DE"));

Upvotes: 0

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56222

You can create custom NumberFormatInfo:

string s = "372551.40";
var dec = decimal.Parse(s, CultureInfo.InvariantCulture);

var nfi = new CultureInfo("en-US", false).NumberFormat;
nfi.NumberGroupSeparator = ".";
nfi.NumberDecimalSeparator = ",";

var res = dec.ToString("n", nfi);
var resDecimal = decimal.Parse(res, nfi);

Output is exactly what you need: 372.551,40

Upvotes: -1

sealz
sealz

Reputation: 5408

You can use .Replace

   string string 1 = "372,551.40";
    string1.Replace(",","");   
    decimalVal = System.Convert.ToDecimal(StringVal);
//shows 372551.40

You can always throw that into a for loop if you are playign with a ton of numbers.

You can find more in depth info and some examples on MSDN

Upvotes: 0

JohnD
JohnD

Reputation: 14787

Something like this?

        string s = "372551.40";
        decimal d;
        if (decimal.TryParse(s, out d))
        {
            var culture = new CultureInfo("de-DE");
            var result = d.ToString("0,0.00", culture);
            // result is "372.551,40"
        }

You can also use the current culture instead of hard-coding one like I did.

Hope this helps,

John

Upvotes: 2

InBetween
InBetween

Reputation: 32790

Do the following:

string s = "372551.40";
CultureInfo cultureInfo = CultureInfo.InvariantCulure; //Use relevant culture in which your number is formatted. In this case InvariantCulture would do.
decimal d;
bool succesful = Decimal.TryParse(s, NumberStyles.Number, cultureInfo, out d); //it will try to parse the string according to the specified culture.;

If you have a succesful parse, then d will store the numeric value represented by s as a decimal value which you can output into any formatted string and culture the ToString() or Format.String().

Note that if the culture in which the number represented by s is the current system culture, then you can use the TryParse(string s, out decimal d) overload where it is not necessary to specify NumberStyles and IFormatProvider.

Upvotes: 2

V4Vendetta
V4Vendetta

Reputation: 38230

The display as you mentioned is dependent on the culture setting.

Make your new CultureInfo and in the NumberFormat, you will have to modify some settings like Decimal Separator as , and Thousands Separator as . and provide this to the ToString method of the variable holding the decimal value.

This should display the value as 372.551,40

Upvotes: 0

Oddmar Dam
Oddmar Dam

Reputation: 719

string value;
Decimal number;

value = "16,523,421";
if (!Decimal.TryParse(value,out number))
{
  // set it to something if the "Value" is not a number
  number = -1;
}

Upvotes: 1

Mario Vernari
Mario Vernari

Reputation: 7306

From MSDN:

string value;
decimal number;
// Parse an integer with thousands separators. 
value = "16,523,421";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays: 
//    16,523,421' converted to 16523421.

Cheers

Upvotes: -1

CheeZe5
CheeZe5

Reputation: 993

Use decimal.Parse() to make it a decimal. Then you have many formatting options.

Upvotes: -1

Related Questions