当前位置:编程学习 > VB >>

Format格式

我想用Format函数把数字变成指定格式。
例如:1000->1,000
1050.2->1,050.2
1000.23->1,000.23

就是说我要加千位分隔符,并且根据实际的位数显示小数

请指教! --------------------编程问答-------------------- formt(val, "#,##0.##") --------------------编程问答-------------------- 这样子的话,如果没有小数的数字,最后会有一个小数点。 --------------------编程问答-------------------- Format(x, "#,#.########")
小数点后最常保留几位,就写几个# --------------------编程问答-------------------- 然后
if right(s,1) = "." then
  s = left(s,len(s)-1)
end if --------------------编程问答--------------------

Private Sub Command1_Click()
Dim Pay As Double
'str=1000->1,000
'1050.2->1,050.2
'1000.23->1,000.23
Pay = Format(1000.23, "##,##0.00")

Debug.Print Pay

End Sub



--------------------编程问答-------------------- dim x as double
x = 1000

dim s as string
s = Format(x, "#,#.########") 
if right(s,1) = "." then 
  s = left(s,len(s)-1) 
end if
--------------------编程问答-------------------- 5楼正解。 --------------------编程问答-------------------- 3,4 楼吧, 楼主不要。00 --------------------编程问答--------------------
引用 2 楼 bigfaint 的回复:
这样子的话,如果没有小数的数字,最后会有一个小数点。


没有发现你说的问题、 --------------------编程问答-------------------- Format(strP,"0,000,000.00") --------------------编程问答--------------------
引用 9 楼 zuoxingyu 的回复:
引用 2 楼 bigfaint 的回复:
这样子的话,如果没有小数的数字,最后会有一个小数点。
没有发现你说的问题、

确实有. --------------------编程问答--------------------
Private Sub Command1_Click()
    Dim a!
    a = 1000
    Debug.Print IIf(Fix(a) = a, a, Format(1000, "##,##0.00"))
End Sub
--------------------编程问答--------------------
引用 2 楼 bigfaint 的回复:
这样子的话,如果没有小数的数字,最后会有一个小数点。
是不是不同版本或DLL的问题?? --------------------编程问答--------------------

   Dim x As Variant
   x = 1000
   x = IIf(Int(x) = x, Format(x, "#,###"), Format(x, "#,##0.####"))
   Debug.Print x
   x = 1050.2
   x = IIf(Int(x) = x, Format(x, "#,###"), Format(x, "#,##0.####"))
   Debug.Print x
   x = 1000.23
   x = IIf(Int(x) = x, Format(x, "#,###"), Format(x, "#,##0.####"))
   Debug.Print x

补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,