2014年1月29日 星期三

字串轉數字的小技巧

最常用的 strtoint() 或是 strtofloat() 或是 strtocurr()
不過這會有個問題
如果使用者輸入的不是數字的話,就會出現錯誤

一般習慣是會用 try except ..來處理
但是Delphi裡面有function可以處理這個問題,不用再使用try except來處理

strtointDef('字串',0);
strtofloatDef('字串',0);
strtocurrDef('字串',0);
後面的0就是, 轉不過去時, 就回傳的數字,可以自訂

exsample:

strtointDef('123',0)  回傳 123
strtointDef('xxx',0)  回傳 0

沒有留言:

張貼留言