is there easier/shorter way convert final comma of currency input decimal.
inputs 2 000,99 or 2,000,99
mysql wants them 2000.99
<cfform action="commatest.cfm?gotime" onsubmit="commatest.cfm" method="post" name="waiv"> <cfinput type="text" name="commer"> <input type="submit" style="width:180px;" value="convertme" class="hide button"> </cfform> <cfif isdefined("gotime")> <!--- start script ---> <cfset formentry = form.commer> <cfset howlong = #len(formentry)#> <cfif howlong gt 3> <cfset leftlen = howlong - 3> <cfset rside = #right(formentry, 3)#> <cfset rside = rereplace(rside,"[,]",".", "all")> <cfset lside = #left(form.commer, leftlen)#> <cfset lside = rereplacenocase(lside,"[-$a-z,]","", "all")> <cfset lside = rereplace(lside, "[[:space:]]", "", "all") /> <cfset newb = #lside# & #rside#> <!--- output database (or webpage in case) ---> <cfoutput> <h1>#newb# (number? #isnumeric(newb)#)</h1> </cfoutput> <cfelse> <cfoutput> <h1>#formentry# (number? #isnumeric(formentry)#)</h1> </cfoutput> </cfif> </cfif>
as usual there several different ways this. this:
<cfscript> source1 = '2 000,99'; source2 = '2,000,99'; // remove commas , spaces example1 = rereplace(source1,"[\s,]","","all"); example2 = rereplace(source2,"[\s,]","","all"); // insert decimal before last 2 digits example1 = insert(".",example1,(len(example1)-2)); example2 = insert(".",example2,(len(example2)-2)); writeoutput(source1 & " = " & example1); writeoutput("<br>"); writeoutput(source2 & " = " & example2); </cfscript>
that code gives following output:
2 000,99 = 2000.99 2,000,99 = 2000.99
of course assumes last 2 digits going after decimal point.
Comments
Post a Comment