同上一篇【原】[Data.Common.Format] 格式化传入的float(浮点型)字符串
步骤:
1. 添加一个用户自定义函数(FormatMinDecimals)
2. 在公式中调用FormatMinDecimals(mailto:%7B@strFloat}, 2)
代码如下(Crystal 语法):
Crystal 语法
Function (StringVar strFloat, NumberVar minDecimals)
NumberVar currentDecimals;
If strFloat = "" Then
strFloat // If strFloat = "" then return ""
Else
(
//When minDecimals is 0 and decimal point is the last character,
//strFloat = "." then change strFloat from "." to "0"
//Else remove the right "." such as "1."
If minDecimals = 0 and Right(strFloat, 1) = "." Then
If strFloat = "." Then
strFloat := "0"
Else
strFloat := Left(strFloat, Len(strFloat)-1);
//if has decimal point
//decimal point is first character, add a zero to the left
//get current decimal digits
If Instr(strFloat, ".") > 0 Then
(
If Instr(strFloat, ".") = 1 Then
strFloat := "0" + strFloat;
//Get current decimal digits
currentDecimals := Len(strFloat) - Instr(strFloat, ".")
)
//if no decimal point and minDecimal > 0
//add ".0" to the right of strFloat
//get current decimal digits
Else If (minDecimals > 0) Then
(
strFloat := strFloat + ".0";
//Get current decimal digits
currentDecimals := Len(strFloat) - Instr(strFloat, ".");
)
//if no decimal point and minDecimal = 0
//set current decimal digits to 0
Else
currentDecimals := 0;
While (Left(strFloat, 1) = "0" and Left(strFloat, 2) <> "0." and strFloat <> "0") Or //remove leading zeros if more than one
(currentDecimals < minDecimals) Or //append one zero "while" has less than min decimal places
(currentDecimals > minDecimals and Right(strFloat, 1) = "0") Do //remove trailing zeros until it has minimum decimal digits
(
//remove leading zeros if more than one
If (Left(strFloat, 1) = "0" and Left(strFloat, 2) <> "0.") Then
strFloat := Right(strFloat, Len(strFloat)-1);
//append one zero "while" has less than min decimal places
If (currentDecimals < minDecimals) Then
(
strFloat := strFloat + "0"
)
//remove trailing zeros until it has minimum decimal digits
Else If(currentDecimals > minDecimals and Right(strFloat, 1) = "0") Then
(
strFloat := Left(strFloat, Len(strFloat)-1);
//remove decimal point if decimal point is last character and minDecimals =0
If (Right(strFloat, 1) = "." and minDecimals = 0) Then
strFloat := Left(strFloat, Len(strFloat)-1)
);
//Get new decimal digits
If Instr(strFloat, ".") > 0 Then
currentDecimals := Len(strFloat) - Instr(strFloat, ".")
Else
currentDecimals := 0
);
//return strFloat
strFloat
)
Function (StringVar strFloat, NumberVar minDecimals)
NumberVar currentDecimals;
If strFloat = "" Then
strFloat // If strFloat = "" then return ""
Else
(
//When minDecimals is 0 and decimal point is the last character,
//strFloat = "." then change strFloat from "." to "0"
//Else remove the right "." such as "1."
If minDecimals = 0 and Right(strFloat, 1) = "." Then
If strFloat = "." Then
strFloat := "0"
Else
strFloat := Left(strFloat, Len(strFloat)-1);
//if has decimal point
//decimal point is first character, add a zero to the left
//get current decimal digits
If Instr(strFloat, ".") > 0 Then
(
If Instr(strFloat, ".") = 1 Then
strFloat := "0" + strFloat;
//Get current decimal digits
currentDecimals := Len(strFloat) - Instr(strFloat, ".")
)
//if no decimal point and minDecimal > 0
//add ".0" to the right of strFloat
//get current decimal digits
Else If (minDecimals > 0) Then
(
strFloat := strFloat + ".0";
//Get current decimal digits
currentDecimals := Len(strFloat) - Instr(strFloat, ".");
)
//if no decimal point and minDecimal = 0
//set current decimal digits to 0
Else
currentDecimals := 0;
While (Left(strFloat, 1) = "0" and Left(strFloat, 2) <> "0." and strFloat <> "0") Or //remove leading zeros if more than one
(currentDecimals < minDecimals) Or //append one zero "while" has less than min decimal places
(currentDecimals > minDecimals and Right(strFloat, 1) = "0") Do //remove trailing zeros until it has minimum decimal digits
(
//remove leading zeros if more than one
If (Left(strFloat, 1) = "0" and Left(strFloat, 2) <> "0.") Then
strFloat := Right(strFloat, Len(strFloat)-1);
//append one zero "while" has less than min decimal places
If (currentDecimals < minDecimals) Then
(
strFloat := strFloat + "0"
)
//remove trailing zeros until it has minimum decimal digits
Else If(currentDecimals > minDecimals and Right(strFloat, 1) = "0") Then
(
strFloat := Left(strFloat, Len(strFloat)-1);
//remove decimal point if decimal point is last character and minDecimals =0
If (Right(strFloat, 1) = "." and minDecimals = 0) Then
strFloat := Left(strFloat, Len(strFloat)-1)
);
//Get new decimal digits
If Instr(strFloat, ".") > 0 Then
currentDecimals := Len(strFloat) - Instr(strFloat, ".")
Else
currentDecimals := 0
);
//return strFloat
strFloat
)