Convert any currency string to double
I think this should work:
double.Parse(currencyValue, NumberStyles.AllowCurrencySymbol | NumberStyles.Currency);
Here you can see more about the NumberStyles.
Edit: In case anyone sees this answer without looking at the other answers/comments, this answer answered the question as written, but storing currency as a double
is not a good idea, and it would be better to use decimal instead.
C# Converting a Currency String to Double
The easiest way to parse the C
format is probably with
Double.Parse(text, System.Globalization.NumberStyles.Currency)
Of course you would always want to use Decimal
to handle currency, and Decimal.Parse
takes the same parameters.
In this case, though, you would want to store your internal numeric values along with their textual representation rather than converting to a string and then parsing back into a number.
Swift convert Currency string to double
NumberFormatter
can convert to and from string. Also Double
cannot represent certain numbers exactly since it's based 2. Using Decimal
is slower but safer.
let str = "$4,102.33"
let formatter = NumberFormatter()
formatter.numberStyle = .currency
if let number = formatter.number(from: str) {
let amount = number.decimalValue
print(amount)
}
how to convert currency format to double in java
I assume you do not want the currency details. In that case, use getNumberInstance() instead of getCurrencyInstance().
Use:
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setGroupingUsed(false);
nf.setMinimumFractionDigits(2);
String ammount= nf.format(value);
Converting different countrys currency to double using java
Never use double
for representing exact amounts
Well, of course you can do, but you need to really understand floating point arithmetic
Use a NumberFormat
. Whilst it does handle some currencies, it's usually easier just to strip all the currency symbols. The NumberFormat
will use Locale
to work out the delimiters to use:
public static BigDecimal parse(final String amount, final Locale locale) throws ParseException {
final NumberFormat format = NumberFormat.getNumberInstance(locale);
if (format instanceof DecimalFormat) {
((DecimalFormat) format).setParseBigDecimal(true);
}
return (BigDecimal) format.parse(amount.replaceAll("[^\\d.,]",""));
}
This takes a String
of the amount and the Locale
. It then creates a BigDecimal
parsing NumberFormat
instance. It uses replaceAll
and regex to strip all but digits, .
and ,
from the number then parses it.
A quick demo against your examples:
public static void main(String[] args) throws ParseException {
final String dollarsA = "$199.00";
final String real = "R$ 399,00";
final String dollarsB = "£25.00";
final String tailingEuro = "90,83 €";
final String dollarsC = "$199.00";
final String dirham = "AED 449.00";
System.out.println(parse(dollarsA, Locale.US));
System.out.println(parse(real, Locale.FRANCE));
System.out.println(parse(dollarsB, Locale.US));
System.out.println(parse(tailingEuro, Locale.FRANCE));
System.out.println(parse(dollarsC, Locale.US));
System.out.println(parse(dirham, Locale.US));
}
Output:
199.00
399.00
25.00
90.83
199.00
449.00
I have simply used US
where the decimal is .
and FRANCE
where the decimal is ,
but you could use the correct Locale
for the currency if you wish.
How to convert a currency string to a double with Javascript?
Remove all non dot / digits:
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
Get double value from currency frormatted string
I don't know either the below solution is perfect or not but it is working according to my requirement.
try {
return NumberFormat.getCurrencyInstance().parse(currency).doubleValue();
} catch (ParseException e) {
e.printStackTrace();
// Currency string is not parsable
// might be different locale
String cleanString = currency.replaceAll("\\D", "");
try {
double money = Double.parseDouble(cleanString);
return money / 100;
} catch (Exception ex) {
ex.printStackTrace();
}
}
return 0;
Convert currency textfield to double in swift 5?
Based on the extensions you have shown in your code, this should work to convert your currency string into a Double
value:
let doubleValue = Double(yourCurrencyString.digits)
Replace yourCurrencyString
with whatever variable you're holding your currency string in.
Convert currency string to decimal?
Here is a method that most closely resembles the code you've provided
public static decimal Parse(string input)
{
return decimal.Parse(Regex.Replace(input, @"[^\d.]", ""));
}
Here is an option that will support negative numbers, and will stop if it finds a second period value, thus reducing the number of strings it returns that are not valid decimal
values. It also has a few other modifications not seen in the OP to handle additional cases your current code doesn't.
public static decimal Parse(string input)
{
return decimal.Parse(Regex.Match(input, @"-?\d{1,3}(,\d{3})*(\.\d+)?").Value);
}
Related Topics
How to Make a Hyperlink Work in a Richtextbox
Connection String with Relative Path to the Database File
Winforms | C# | Autocomplete in the Middle of a Textbox
Can't Get SQL Server Compact 3.5/4 to Work with Asp .Net MVC 2
C# Testing to See If a String Is an Integer
How to Get the Member to Which My Custom Attribute Was Applied
How to Override List<T>'s Add Method in C#
Can't Convert Value Type Array to Params Object[]
Loading a Full Hierarchy from a Self Referencing Table with Entityframework.Core
How to Read a File Which Is in Use
What Is Imex Within Oledb Connection Strings
Retry a Task Multiple Times Based on User Input in Case of an Exception in Task
Programmatically Adding Images to Rtf Document
What Is the "Right" Way to Bring a Windows Forms Application to the Foreground