【原】Crystal Reports水晶报表 格式化传入的float字符串

同上一篇【原】[Data.Common.Format] 格式化传入的float(浮点型)字符串

步骤:

1. 添加一个用户自定义函数(FormatMinDecimals)

2. 在公式中调用FormatMinDecimals(mailto:%7B@strFloat}, 2)

代码如下(Crystal 语法): 

ContractedBlock.gif ExpandedBlockStart.gif 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
)

转载于:https://www.cnblogs.com/luckylei66/archive/2009/02/12/1389255.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值