我的十进制值有问题 [英] problems with my decimal value

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

问题描述

伙计们

HI guys

km =Convert.ToDecimal(txtKMPerHr.Text);




foreach(decimal v in km.ToString())

{

km = v;

total = rate * km;


lblToTalValue.Text = total.ToString();




}





decimal v has not been assigned anywhere but every time v become 50 when code comes in the foreach...Y is this hapening?

推荐答案

什么是复杂的(而且非常非常愚蠢)
foreach循环循环,一次在in关键字的右边提供项目列表中的每个项目.每次在变量的左侧声明该变量v时,都会提供该项目.

在您的情况下,km.ToSting()返回一个string,当它被视为迭代器时,被任何东西(包括foreach)重新调谐char -的序列,因此:
What is happing is complicated (and very, very silly)
foreach loops round providing one at a time each of the items in the list of items to teh right of the in keyword. Each time round, the item is provided in the variable v which is declared on the left side of the in keyword.

In you case, km.ToSting() returns a string, which when treated as an iterator =by anything (including foreach) retunrs a sequence of char - so:
foreach (char c in "hello")
    {
    Console.WriteLine(c);
    }

将给您:

h
e
l
l
o

您的代码将char强制为decimal,因此原始decimal值中的每个数字都将作为v中的decimal交给您.您在txtKMPerHr.Text中的数字以''开头2'',因此传递给循环的第一个值是50-字符"2"的ASCII值

正如我所说-非常愚蠢.但是,您到底想做什么,您认为这会有所帮助? :laugh:

Your code forces the char to be a decimal so each digit in the original decimal value is handed to you as a decimal in v. Your number in txtKMPerHr.Text starts with ''2'', so the first value passed to the loop is 50 - ASCII value of the character ''2''

As I said - very silly. But what of earth are you trying to do that you think that this would help? :laugh:


这篇关于我的十进制值有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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