fork download
  1. object Main extends App {
  2. val value = "\nValue is €2,927.18 corresponding to 27,163.88kr"
  3. val pattern = "(?=[$£€¥₹])(.)(\\d{1,3}(?:,\\d{3})?(?:\\.\\d+)?)|(\\d{1,3}(?:,\\d{3})?(?:\\.\\d+)?)(kr\\.?|Kč)".r
  4.  
  5. pattern.findAllIn(value).matchData foreach {
  6. m => {
  7. val c = if (m.group(1)==null) m.group(4) else m.group(1)
  8. val a = if (m.group(1)==null) m.group(3) else m.group(2)
  9. println("Currency = " + c )
  10. println("Amount = " + a )
  11.  
  12. val str = a.replaceAll(",", "")
  13.  
  14. // convert it to an integer and/or double.
  15. val d = str.toDouble
  16. println("Double value = " + d)
  17. println(" type = " + d.getClass())
  18.  
  19. val i = d.toInt
  20. println("Integer value = " + i)
  21. println(" type = " + i.getClass())
  22. }
  23. }
  24. }
Success #stdin #stdout 0.36s 322496KB
stdin
Standard input is empty
stdout
Currency      = €
Amount        = 2,927.18
Double value  = 2927.18
         type = double
Integer value = 2927
         type = int
Currency      = kr
Amount        = 27,163.88
Double value  = 27163.88
         type = double
Integer value = 27163
         type = int