I only pay with dollars
Python 3.6 (pre-release), 87
lambda s:f"${int(s.translate({46:'',44:''})[1:])*[110,15,0,137][ord(s[0])%4]/1e4:,.2f}"
Uses f-strings to evaluate the result and format it.
s.translate({46:'',44:''})
removes dots and commas from s
, thus making it a valid int
literal, then int(...)
converts it into the actual int
object.
Convex, 56 55 54 bytes
(\®\.|,"ö)\e_\'.\++~\"€£¥"#[1.1_.27+.15]=*"%,.2f"\Ø'$\
Well, this can definitely be shortened. Try it online!
Saved a byte thanks to Lynn!
Explanation to come when I can get access to a computer.
Java 7, 240 227 215 211 207 202 199 196 bytes
(201 - 2 bytes because of the rule "If you use one of the above symbols (€, £, ¥), you may count them as 1 byte")
Thanks to @Frozn for saving a lot of bytes.
String c(String a){int c=a.charAt(0);return java.text.NumberFormat.getCurrencyInstance(java.util.Locale.US).format(new Long(a.substring(1).replaceAll(",|\\.",""))*(c<'¥'?1.37:c>'¥'?1.1:.15)/100);}
Ungolfed & test code:
Try it here.
class Main{
static String c(String a){
int c = a.charAt(0);
return java.text.NumberFormat.getCurrencyInstance(java.util.Locale.US)
.format(new Long(a.substring(1).replaceAll(",|\\.","")) *
(c < '¥'
? 1.37
: c > '¥'
? 1.1
: .15
) / 100);
}
public static void main(String[] a){
System.out.println(c("€1,37"));
System.out.println(c("£4.00"));
System.out.println(c("¥2,782,122.78"));
}
}
Output:
$1.51
$5.48
$417,318.42