|
晚上想了一下,写了个虽然还是繁琐但条理比较清楚的函数,我试了一下,我试过的数据暂未出现错误,不过那个数要为字符型(string,可看后面例子),即使你输入的数字是中文也可以实现本效果,望能给你帮上忙。function tst(num as string,n as integer) as stringdim x,y,z,numlngdim expnum as stringdim i as longdim m() as byteif n<1 thenmsgbox "啥也不留,计算结果给空气啊?",vbokonly,"别耍我了,GM!"exit functionend ifx=val(num)tst=iif(x>=0,"+","-")y=abs(x)if x=0 then tst=0exit functionend ifif y>=1 thenm=strconv(z,vbfromunicode)for i=1 to ubound(m)if m(i)=46 thenexit forend ifnextexpnum=iif(i>1,"E+" & trim(str(i-1)),"")z=y/(10^(i-1))elsem=strconv("0" & z,vbfromunicode)for i=0 to uboun(m)if m(i)>=49 thenexpnum="E-" & trim(str(i-1))z=y*10^(i-1)exit forend ifnextend ifnumlng=len(trim(str(z)))-1if numlng=1 thentst=tst & trim(str(z)) & iif(n>1,".","") & string(n-1,"0")exit functionend ifz=int((z*(10^(n-1))+0.5))/10^(n-1)if n<=numlng thentst=tst & trim(str(z)) & expnumexit functionelsetst=tst & trim(str(z)) & string(n-numlng,"0" & expnumend ifend function'tst("-0.0002",4)的结果为2.000E-4,数字最好加上引号'tst("-123.456789。中国",5)的结果为-123.46E+2'如果对0的结果要求为0.000之类,可删除‘if x=0 then ’tst=0‘exit function |
|