0

是否有更简单/更短的方法将货币输入的最终逗号转换为小数。

输入看起来像 2 000,99 或 2,000,99

MySQL 希望它们看起来像 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 TO DATABASE (or webpage in this case) --->
<cfoutput> 
<h1>#newb# (number? #IsNumeric(newb)#)</h1>
</cfoutput>

<cfelse>

<cfoutput>
<h1>#formentry# (number? #IsNumeric(formentry)#)</h1>
</cfoutput>

</cfif>
</cfif>
4

1 回答 1

1

像往常一样,有几种不同的方法可以做到这一点。我可能会做这样的事情:

<cfscript>
source1 = '2 000,99';
source2 = '2,000,99';

// remove ALL commas and spaces
example1 = REReplace(source1,"[\s,]","","all");
example2 = REReplace(source2,"[\s,]","","all");

// insert a decimal before the last two digits
example1 = Insert(".",example1,(Len(example1)-2));
example2 = Insert(".",example2,(Len(example2)-2));

writeOutput(source1 & " = " & example1);
writeOutput("<br>");
writeOutput(source2 & " = " & example2);
</cfscript>

该代码给出以下输出:

2 000,99 = 2000.99
2,000,99 = 2000.99

当然,这假设最后两位数字总是在小数点之后。

这是上面代码的要点

于 2017-04-05T19:07:51.430 回答