0

how to convert string to decimal upto 3 places?

my string will be like this

striing aa = "22.333"
string  bb = "22"
string cc = "22.4444"

how to convert this to decimal

leppie
  • 115,091
  • 17
  • 196
  • 297
Bhavin Bhaskaran
  • 572
  • 5
  • 17
  • 41

6 Answers6

2

You can convert and round in one line - and one line to take care of localization:

string cc = "22.4444";
IFormatProvider provider = System.Globalization.CultureInfo.InvariantCulture;
decimal ccDecimal = Math.Round(Convert.ToDecimal(cc,provider), 3);

Will return 22.444.

Gustav
  • 53,498
  • 7
  • 29
  • 55
0

You can do it with decimal.Parse (ToDecimal) Method: https://msdn.microsoft.com/de-de/library/hf9z3s65%28v=vs.110%29.aspx

string value = "22.333";
decimal d = decimal.Parse(s);
Luca
  • 1,766
  • 3
  • 27
  • 38
  • i m stucked here. pls help. i will lose jobe for this today .. new to asp.net i need to know it for 3 decimal places. my brain is blank now.. – – Bhavin Bhaskaran Jun 18 '15 at 11:41
  • Than you can use the TryParse Method: value = "22.333"; if (Decimal.TryParse(value, out number)) Console.WriteLine(number); else Console.WriteLine("Unable to parse '{0}'.", value); – Luca Jun 18 '15 at 11:44
  • 1
    This code is *not* safe - it will fail in cultures that use `,` as a decimal point. A lot of European countries do. The compiler will even give a warning about this. The duplicate question shows the correct way to parse using the InvariantCulture – Panagiotis Kanavos Jun 18 '15 at 12:49
0

First you can convert your String to a Decimal Value:

var convertDecimal = Convert.ToDecimal(value);

Afterwards you can round it up, if it is longer:
https://msdn.microsoft.com/en-us/library/system.math.round(v=vs.110).aspx

As far as i know, you cant "convert" the String to a decimal with 3 Positions, if its smaller, because it is saved with the zeros anyway

Xlaech
  • 456
  • 3
  • 19
  • 1
    This code is *not* safe - it will fail in cultures that use `,` as a decimal point. A lot of European countries do. The compiler will even give a warning about this. The duplicate question shows the correct way to parse using the InvariantCulture – Panagiotis Kanavos Jun 18 '15 at 12:50
  • The other solution just surrounds the statement with a try-catch block. For me they where not part of the solution, because it is a general Problem your might encounter when parsing non-basic types. But you are write. I edit the answer. – Xlaech Jun 18 '15 at 14:37
  • No other answer uses try/catch. The accepted answer in the duplicate uses CultureInfo.InvariantCulture. In fact, both the compiler *and* Resharper give a warning with such code, suggesting to use `CultureInfo.InvariantCulture` – Panagiotis Kanavos Jun 18 '15 at 14:46
  • a i see i was to fast... – Xlaech Jun 18 '15 at 14:48
0

3 places can be achieved with Math.Round. Safest string conversion would be the TryParse method:

 string srcString = "123.45123";

 decimal outDecimal;

 if (decimal.TryParse(srcString, out outDecimal))
 {
     // conversion is successful, so you can use outDecimal variable  
     outDecimal = decimal.Round(outDecimal, 3, MidpointRounding.AwayFromZero); 
 }
Charles Mager
  • 25,735
  • 2
  • 35
  • 45
yurart
  • 593
  • 1
  • 6
  • 23
  • 2
    This code is *not* safe - it will fail in cultures that use `,` as a decimal point. A lot of European countries do. The compiler will even give a warning about this. The duplicate question shows the correct way to parse using the InvariantCulture – Panagiotis Kanavos Jun 18 '15 at 12:48
  • Yes, I forgot about culture – yurart Jun 18 '15 at 15:24
0

You could doing something like this

    string aa = "22.333";
    string bb = "22";
    string cc = "22.4444";

    decimal d = decimal.Parse(aa);
    decimal e = decimal.Parse(bb);
    decimal f = decimal.Parse(cc);

    Console.WriteLine(string.Format("{0:0.000}", d));
    Console.WriteLine(string.Format("{0:0.000}", e));
    Console.WriteLine(string.Format("{0:0.000}", f));
Gane
  • 120
  • 11
  • Well, you should test it. It won't. You can't format a string using a number format: https://dotnetfiddle.net/gIC4oS – Charles Mager Jun 18 '15 at 12:05
  • 1
    This code is *not* safe - it will fail in cultures that use `,` as a decimal point. A lot of European countries do. The compiler will even give a warning about this. The duplicate question shows the correct way to parse using the InvariantCulture – Panagiotis Kanavos Jun 18 '15 at 12:50
0

try it

string value;
decimal number;

value = "1,643.57";
if (Decimal.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("Unable to parse '{0}'.", value);
  • 1
    This code is *not* safe - it will fail in cultures that use `,` as a decimal point. A lot of European countries do. The compiler will even give a warning about this. The duplicate question shows the correct way to parse using the InvariantCulture – Panagiotis Kanavos Jun 18 '15 at 12:48