I have a label (lblAmountTendered) which contains Currency String in it. I would like to convert it into double to perform some calculation. However, it shown an Error Message: Input string was not in a correct format in the first statement.
Here is my code:
double balance = double.Parse(amount) - double.Parse(lblAmountTendered.Text.ToString());
lblBalanceDue.Text = balance.ToString("c2",CultureInfo.CreateSpecificCulture("en-MY"));
For Example:
lblAmountTendered = RM 15
I want to retrieve the value of it (15) for calculation.
Looking forward for the solution. I appreciate for your help! :)
PROBLEM SOLVED
lblAmountTendered.Text.ToString(). Remove(0,3)Remove(0,3) helps us to remove 'RM' from 'RM 15', so we can convert it to double or float easily as shown in below:
float.Parse (lblAmountTendered.Text.ToString(). Remove(0, 3) )
Here you first need to retrieve the numeric value from a string using below regex:
string resultNum = Regex.Match(lblAmountTendered.Text, @"\d+").Value;
and then use resultNum
in your code like-
double balance = double.Parse(amount.ToString()) - double.Parse(resultNum);
lblBalanceDue.Text = balance.ToString("c2",CultureInfo.CreateSpecificCulture("en-MY"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.