0
String ItemName=txtItemName.Text.ToString();
String ItemSpecification=txtItemSpecification.Text.ToString ();
String Category=cmbCategory.SelectedIndex .ToString();
String UnitOfMeasurement=cmbUnitOfMeasurement .SelectedIndex .ToString ();
Decimal Reorderlevel = txtReorderLevel.Text.();
Decimal LifeOfSpan=0;

String NeedApproval=rblNeedApprovalOrNotdx.SelectedItem.Value.ToString();
String Description=MemoDescription.Text.ToString ();

For string I can get a value using ToString. How can I get it for decimal?

Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
  • Syntax error in question itself, it should be Decimal Reorderlevel = txtReorderLevel.Text(); instead of Decimal Reorderlevel = txtReorderLevel.Text.(); – Pandiyan Cool Dec 12 '13 at 05:01

4 Answers4

0

Use Convert.ToDecimal() method to convert string to decimal.

The below code prints the value as 1200.00.

var convertDecimal = Convert.ToDecimal("1200.00");

Just in case it's cultural issues, try this version which shouldn't depend on your locale at all:

decimal d = decimal.Parse("1200.00", CultureInfo.InvariantCulture);
Console.WriteLine(d.ToString(CultureInfo.InvariantCulture));

I have always preferred using the Double.TryParse methods because it is going to spit back success or failure to convert without having to worry about exceptions.

string value;
double number;

value = Double.MinValue.ToString();
if (Double.TryParse(value, out number))
   Console.WriteLine(number);
else
   Console.WriteLine("{0} is outside the range of a Double.", 
                     value);
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
0

Use Decimal.TryParse(), like this:

decimal Reorderlevel;
if (Decimal.TryParse(UnitOfMeasurement, out Reorderlevel))
{
    // Conversion from string to decimal succeeded

}
else
{
    // Conversion failed

}

Note: TryParse() does not thrown an exception if the attempted cast from string to decimal fails, but rather returns false when it fails and true when it succeeds.


UPDATE:

If you prefer to use a nullable decimal, then do this:

decimal tempValue;
decimal? Reorderlevel = Decimal.TryParse(UnitOfMeasurement, out tmpvalue) ?
              tmpvalue : (decimal?)null;

Then you can check if Reorderlevel has a value before using it, like this:

if(Reorderlevel.HasValue)
{
    // Do something with reorder level value here

}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • first understand what i need for string we can use as ToString(),like wise for Decimal how to get this? String UnitOfMeasurement=cmbUnitOfMeasurement .SelectedIndex .ToString (); Decimal Reorderlevel = txtReorderLevel.Text.(); – user3036427 Dec 12 '13 at 05:02
  • @user3036427 - updated answer to use the `Reorderlevel` variable and `UnitOfMeasurement` string value. – Karl Anderson Dec 12 '13 at 05:06
0

Use Convert.ToDecimal() or Decimal.TryParse()

Ravi Patel
  • 451
  • 1
  • 6
  • 17
0

You can use the method Convert.ToDecimal() from the System namespace.

0xCAFEBABE
  • 5,576
  • 5
  • 34
  • 59
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115