Vb如何将.1减去一个数字? [英] Vb how do I deduct .1 to a number?

查看:111
本文介绍了Vb如何将.1减去一个数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我有0.093,其输出应为0.092。另一个例子,如果我有0.15它应该是0.14,依此类推。我试试这个代码:但是0.093的结果是-0.007



我试过的:



dim结果为整数

result = val(orig.text) - .1

for example i have 0.093 its output should be 0.092. another example if i have 0.15 it should be 0.14 and so on for others. i try this code: but the result for 0.093 is -0.007

What I have tried:

dim result as integer
result = val(orig.text) - .1

推荐答案

那不行要像你想象的一样简单:将最低有效数字减少一个对于整数值是可以的,但浮点值不会以与它完全兼容的方式存储:因为它们存储为二进制,它们只是近似你看到的价值,而不是真正持有实际价值。



你所展示的代码存在问题:整数根本不存在数字的派系部分,所以你的两个例子数字都会给出零结果。



但是......你试图减少用户输入字符串的最低有效位数,实际上使这个过程更容易!

直接使用字符串,你可以这样做:

1)识别索引o至少有效数字:最初是字符串的长度减去一。

2)将字符串转换为字符数组

3)现在循环。

3.1)查看索引处的字符。它是零吗?

3.2)如果是,则将其设为9,将索引递减1,然后继续循环。

3.3)否则,是否为a期间?

3.3.1)如果是,则将索引递减一,然后继续循环。

3.3.2)否则,减少字符买一,然后退出循环。

4)循环之后,将char数组转换回字符串,并将其转换为Double
That's not going to be as simple as your think: reducing the least significant digit by one is ok for integer values, but floating point values are not stored in a way which it totally compatible with that: since they are stored as binary, they only "approximate" to the value you see, rather than genuinely holding the actual value.

And the code you show has problems: integers do not hold the factional part of a number at all, so your two example numbers would both give a result of zero.

But ... you are trying to reduce the least significant digit of a user entered string, which actually makes the process easier!
Work with the string directly, and you can do it:
1) Identify the index of least the significant digit: initially that is the length of the string minus one.
2) Convert the string to a char array
3) Now loop.
3.1) Look at the character at the index. Is it zero?
3.2) If it is, make it a nine, decrement the index by one, and continue with the loop.
3.3) Otherwise, is it a period?
3.3.1) If it is, decrement the index by one, and continue with the loop.
3.3.2) Otherwise, reduce the character buy one, and exit the loop.
4) After the loop, convert the char array back to a string, and convert it to a Double


据我所知,你需要从分数结尾处出现的数字的面值中减去1。



要做到这一点,首先,你必须找到分数中的小数位置,然后从数字中减去1/10 ^ positionOfDecimal。



示例代码。

From what I understand, you need to subtract 1 from the face value of the digit that occurs at the end of a fraction.

To do this, first, you'll have to find the position of the decimal in the fraction, then subtract 1/10^positionOfDecimal from the number.

Example code.
Dim result As Interger
Dim indexOfDecimalPoint As Integer = orig.text.IndexOf(".")
Dim numberOfDecimals As Integer =  orig.text.Substring(indexOfDecimalPoint + 1).Length

result = Val(orig.text) - 1/(10^numberOfDecimals)





上面的代码应该是您正在尝试的。



The above code should do what you're trying.


这篇关于Vb如何将.1减去一个数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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