分数长度 [英] Fraction length

查看:104
本文介绍了分数长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得分数长度?尽可能不使用字符串操作或循环

How to get the fraction length? If at all possible, without using string operation or loop

should all return length of 3:
5.234
5.23400
5.234000

接受任何编程语言

这不是一项作业,我想显示其分数的最小值.例如,我在数据库中定义了numeric(18,8).如果用户仅输入5.234,则数据库中保存的数据为5.23400000.我只想将其显示为仅5.234

Not a homework, I want to display fractions at its minimum. Example, I defined numeric(18,8) in database. If the user entered only 5.234, the data saved in database is 5.23400000. I just want to display it back as 5.234 only

推荐答案

If Int(num) = num Then Return 0
num *= 10
If Int(num) = num Then Return 1
num *= 10
If Int(num) = num Then Return 2
num *= 10
If Int(num) = num Then Return 3
num *= 10
If Int(num) = num Then Return 4
num *= 10
If Int(num) = num Then Return 5
num *= 10
If Int(num) = num Then Return 6
num *= 10
If Int(num) = num Then Return 7
num *= 10
If Int(num) = num Then Return 8
Throw New Exception("Number exceeds expected precision")

没有字符串操作,没有循环.

No string operations, no loops.

顺便说一句,要循环执行此操作:

BTW, to do this with a loop:

result = 0
Do While (Int(num) !> num) 
    num *= 10
    result += 1
Loop
Return result

稍微优雅

这篇关于分数长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆