Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Bracket (negative) to double

Tags:

c#

Hello How do i Convert a negative value that's in bracket format to double. Currently I have this.

Payment.Text = Calc_Payment().ToString("#,##0.00;(#,##0.00)");

That converts the payment to Bracket format But I want to do the reverse. String in bracket format to Double. If anyone could help please.

like image 976
Master Avatar asked Jan 11 '23 06:01

Master


1 Answers

Please try the following:

// using System.Globalization
double d = double.Parse("(1,000.90)", NumberStyles.AllowParentheses | 
                                      NumberStyles.AllowThousands | 
                                      NumberStyles.AllowDecimalPoint)

/* d = -1000.9 */

Ref. Double.Parse Method (String, NumberStyles); NumberStyles Enumeration

NOTE: Money values are better handled by the 'decimal' type. From the documentation:

Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

like image 109
Wagner DosAnjos Avatar answered Jan 22 '23 07:01

Wagner DosAnjos