凤来仪

专业的计算机学习网站

首页 > ASP显示小数点前面的0

ASP显示小数点前面的0

点击:1079 发布时间:

    ASP显示小数点前面的0的方法:

    一、使用FormatNumber函数。   

dim Price
Price=0.8
response.write FormatNumber(Price,1)
'显示结果:.8

'利用 FormatNumber 函数把数值格式化为带两位小数点
dim Price
Price=0.8
response.write FormatNumber(Price,2)
'显示结果:.80

'利用 FormatNumber 函数把数值格式化为小数点前面有0
dim Price
Price=0.8
response.write FormatNumber(Price,1,-1)
'显示结果:0.8

二、使用自定义函数

Function FmtNumber(nums)
    nums = cstr(nums)    '转换为字符
    if instr(nums,".") > 0 then  '如果中间有点号就说明是数字
        if left(nums,1) = "." then '如果截取的第一个符号是点号,就说明点号前面的0被省略了
            nums = "0" & nums
        else
            nums = nums    '这里验证就是第一个符号不是点号,就直接显示数据
        end if
    else
        nums = nums     '没有点号直接显示数据
    end if
    FmtNumber = nums
End Function

dim Price
Price=0.8
Response.Write FmtNumber(Price)