Groovy web console

subscribe to the feed Subscribe
to this
site
getValue (via #groovywebconsole)
tweet this snippet Tweet
this
script

getValue

Published 9 months ago by Rn7 with tags value
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
String format(String i, int s){
    new String(new char[(s - i.length()) > 0 ? (s - i.length()) : 0]).replace("\0", ' ') + i
}

BigDecimal getValue(String val, BigDecimal max = 1000000){    BigDecimal value
    val = val.replaceAll(/[^\d,.]/, '')
         if(val ==~ /^\d+$/             ){print(" 0 - ");value = new BigDecimal(val)}
    else if(val ==~ /^\d+\.\d{0,2}$/    ){print(" 1 - ");value = new BigDecimal(val)}
    else if(val ==~ /^\d*\.\d{1,2}$/    ){print(" 2 - ");value = new BigDecimal(val)}
    else if(val ==~ /^\.\d{3,}$/        ){print(" 3 - ");value = new BigDecimal(val)}
    else if(val ==~ /^\d+\.\d{3,}$/     ){print(" 4 - ");value = new BigDecimal(val.replaceAll(/\./,''))}
    else if(val ==~ /^\d+,\d{0,2}$/     ){print(" 5 - ");value = new BigDecimal(val.replaceAll(',','.'))}
    else if(val ==~ /^\d*,\d{1,2}$/     ){print(" 6 - ");value = new BigDecimal(val.replaceAll(',','.'))}
    else if(val ==~ /^,\d{3,}$/         ){print(" 7 - ");value = new BigDecimal(val.replaceAll(',','.'))}
    else if(val ==~ /^\d+,\d{3,}$/      ){print(" 8 - ");value = new BigDecimal(val.replaceAll(',','')) }
    else if(val ==~ /^[\d,]+\.\d{0,2}$/ ){print(" 9 - ");value = new BigDecimal(val.replaceAll(',','')) }
    else if(val ==~ /^[\d,]*\.\d{1,2}$/ ){print("10 - ");value = new BigDecimal(val.replaceAll(',','')) }
    else if(val ==~ /^[\d,]*\d\.\d{3,}$/  ){print("11 - ");value = new BigDecimal(val.replaceAll(',','')) }
    else if(val ==~ /^[\d.]*\d,\d{0,2}$/  ){print("12 - ");value = new BigDecimal(val.replaceAll(/\./,'').replaceAll(',','.'))}
    else if(val ==~ /^[\d.]*\d,\d{1,2}$/  ){print("13 - ");value = new BigDecimal(val.replaceAll(/\./,'').replaceAll(',','.'))}
    else if(val ==~ /^[\d.]*\d,\d{3,}$/   ){print("14 - ");value = new BigDecimal(val.replaceAll(/\./,'').replaceAll(',','')) }
    else { print(" x - ");value = 0 }
    value  = value > max ? max : value
    value  = value  < 0 ? 0 : value
    value
}


["2000.00", "2000.0", "2000.", "2000", ".00", ".05", ".052", "2.000,00", "20.0,0", "2.000,", ",00", ",05", ",052 ", "2,000.00", "20,00.0", "2,000.", ",.00", ".05", ".052", "2000,00", "200,0", "2.000,", ",00", ",05", ",052", "R\$ 2000.00", "R\$ 2000.0", "R\$ 2000.", "R\$ 2000", "R\$ .00", "R\$ .05", "R\$ .052", "R\$ 2.000,00", "R\$ 20.0,0", "R\$ 2.000,", "R\$ ,00", "R\$ ,05", "R\$ ,052 ", "R\$ 2,000.00", "R\$ 20,00.0", "R\$ 2,000.", "R\$ ,.00", "R\$ .05", "R\$ .052", "R\$ 2000,00", "R\$ 200,0", "R\$ 2.000,", "R\$ ,00", "R\$ ,05", "R\$ ,052", "15000", "15.000", "15.000,00", "15,000", "15,000.00", ",.12", ",.123", ",2.123"].each{
    println(format(it, 15) +" -> "+ getValue(it))
}
null