孙展波:实现.NET无限潜力

Realize .NET Potential (中文版)
随笔 - 430, 评论 - 12203, 引用 - 54

导航

关于

贴子以"现状"提供且没有任何担保也没有授予任何权利。
计数器(2005/12/8起).点击阅读我的Blog In English

标签

每月存档

最新留言

广告

 

C#中的float是CTS(Common Type System)的System.Single的别名。在VB.NET中,同样的数据类型就命名为Single.有用户发现下面这条指令: Console.WriteLine(1000.09009F.ToString("F5")); 得到的结果是1000.09000而不是显而易见的1000.09009.为什么?

原因是System.Single的精度有限,是7位有效数字。在这样的精度范畴里面,上面的输出结果是正确的。如果需要更高的精度,可以考虑 System.Double或者Decimal.

参考:Decimal Constructor (Single)Single Structure.

贴子以"现状"提供且没有任何担保也没有授予任何权利。

打印 | 张贴于 2004-04-15 06:51:00 | Tag:FCL | 库函数

留言反馈

#uxjfibie - Google Search 编辑
uxjfibie - Google Search
2008-09-22 15:59:26 | [匿名:]
#buy tablet tramadol 编辑
Electricity is actually made up of extremely tiny particles called electrons, that you cannot see with the naked eye unless you have been drinking.
2007-03-30 08:01:00 | [匿名:buy tablet tramadol]
#levitra 编辑
Everything has its wonders, even darkness and silence, and I learn whatever state I am in, therin to be content
2007-03-29 16:13:00 | [匿名:levitra]
#vitamin 编辑
You couldn't even prove the White House staff sane beyond a reasonable doubt.
2007-03-29 12:33:00 | [匿名:vitamin]
#cvs pharmacy 编辑
Do something. If it doesn't work, do something else. No idea is too crazy.
2007-03-29 08:55:00 | [匿名:cvs pharmacy]
#xenical 编辑
What some people mistake for the high cost of living is really the cost of high living.
2007-03-29 02:11:00 | [匿名:xenical]
#hoodia 编辑
I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it.
2007-03-28 23:04:00 | [匿名:hoodia]
#alprazolam 编辑
He's simply got the instinct for being unhappy highly developed.
2007-03-26 16:19:00 | [匿名:alprazolam]
#fioricet online 编辑
It is not enough to succeed. Others must fail.
2007-03-26 12:00:00 | [匿名:fioricet online]
#xenical 编辑
Feet, why do I need them if I have wings to fly?
2007-03-24 01:09:00 | [匿名:xenical]
#hydrocodone 编辑
The release of atomic energy has not created a new problem. It has merely made more urgent the necessity of solving an existing one.
2007-03-23 21:19:00 | [匿名:hydrocodone]
#meridia 编辑
The moment we begin to fear the opinions of others and hesitate to tell the truth that is in us, and from motives of policy are silent when we should speak, the divine floods of light and life no longer flow into our souls.
2007-03-23 17:48:00 | [匿名:meridia]
#levitra 编辑
No.
2007-03-23 14:39:00 | [匿名:levitra]
#paxil 编辑
The cloning of humans is on most of the lists of things to worry about from Science, along with behaviour control, genetic engineering, transplanted heads, computer poetry and the unrestrained growth of plastic flowers.
2007-03-23 10:57:00 | [匿名:paxil]
#valium 编辑
Hares can gambol over the body of a dead lion.
2007-03-23 06:43:00 | [匿名:valium]
#nexium 编辑
The more things a man is ashamed of, the more respectable he is.
2007-03-22 23:50:00 | [匿名:nexium]
#nexium 编辑
The more things a man is ashamed of, the more respectable he is.
2007-03-22 23:50:00 | [匿名:nexium]
#fioricet online 编辑
Life is consciousness.
2007-03-22 16:42:00 | [匿名:fioricet online]
#adipex 编辑
What other dungeon is so dark as one's own heart! What jailer so inexorable as one's self!
2007-03-22 09:35:00 | [匿名:adipex]
#health 编辑
Failures are finger posts on the road to achievement.
2007-03-22 02:57:00 | [匿名:health]
#nexium illusionist honeydew 编辑
Over the years your bodies become walking autobiographies, telling friends and strangers alike of the minor and major stresses of your lives.
2007-03-21 19:50:00 | [匿名:nexium illusionist honeydew]
#valium bannerol bradsot 编辑
The best thing to give to your enemy is forgiveness; to an opponent, tolerance; to a friend, your heart; to your child, a good example; to a father, deference; to your mother, conduct that will make her proud of you; to yourself, respect; to all men, charity.
2007-03-21 16:19:00 | [匿名:valium bannerol bradsot]
#valium bannerol bradsot 编辑
The best thing to give to your enemy is forgiveness; to an opponent, tolerance; to a friend, your heart; to your child, a good example; to a father, deference; to your mother, conduct that will make her proud of you; to yourself, respect; to all men, charity.
2007-03-21 16:19:00 | [匿名:valium bannerol bradsot]
#viagra or cialis storied morfolep 编辑
A tactical retreat is not a bad response to a surprise assault, you know. First you survive. Then you choose your own ground. Then you counterattack.
2007-03-21 11:55:00 | [匿名:viagra or cialis storied morfole]
#adipex 编辑
Only dull people are brilliant at breakfast.
2007-03-21 05:20:00 | [匿名:adipex]
#weight loss 编辑
Famous remarks are very seldom quoted correctly.
2007-03-21 01:39:00 | [匿名:weight loss]
#cheap cialis 编辑
Nobody can be exactly like me. Sometimes even I have trouble doing it.
2007-03-20 22:29:00 | [匿名:cheap cialis]
#pharmacy hemocytoblast ovicell 编辑
Reveal not every secret you have to a friend, for how can you tell but that friend may hereafter become an enemy. And bring not all mischief you are able to upon an enemy, for he may one day become your friend.
2007-03-20 08:36:00 | [匿名:pharmacy hemocytoblast ovicell]
#phentermine adipex 编辑
If there is one thing worse than being an ugly duckling in a house of swans, it's having the swans pretend there's no difference.
2007-03-09 08:24:00 | [匿名:phentermine adipex]
#phentermine adipex 编辑
The meeting of two personalities is like the contact of two chemical substances: if there is any reaction, both are transformed.
2007-03-06 14:09:00 | [匿名:phentermine adipex]
#phentermine adipex 编辑
The meeting of two personalities is like the contact of two chemical substances: if there is any reaction, both are transformed.
2007-03-06 14:08:00 | [匿名:phentermine adipex]
#回复: 1000.09009F.ToString("F5")得到1000.09000 编辑
16867611的转换是不是看第25是什么1的话就进一~0的话就简单舍弃,
不知道是不是这样的,写个例子:
public class test{
public static void Main(string[] arg){
//Console.WriteLine("s={0}",s);
float a=float.Parse("67108863");
float a=float.Parse("67108862");
Console.WriteLine("a={0}",(int)a);
Console.WriteLine("b={0}",(int)b);

}
}
结果都是67108864
2004-06-14 18:55:00 | [匿名:zyh]
#SingleDoubleDecimal 编辑
?1000.09009F.ToString("F5")õ1000.09000 <br/>
<br/>
е?и? <br/>
<br/>
??¾?в???????ÿ?ССô?...2^-1 + 2^-2 + 2^-3 <br/>
<br/>
singledouble??ÿ?λ?????0.7...
2004-04-15 17:13:00 | [匿名:SingleDoubleDecimal]
#回复: 1000.09009F.ToString("F5")得到1000.09000 编辑
我注意到了在VS.NET的环境下,检查f的值是显示16867612。
我不知道确切的原因是什么。
2004-04-15 16:29:00 | [匿名:孙展波]
#回复: 1000.09009F.ToString("F5")得到1000.09000 编辑
(1) float f = float.Parse("16867611");
Console.Writeline(f);

得到1.686761E+07

这个结果符合我们已经知道的float的7位有效数字。

(2) System.Single(即float)和System.Int32(即int)都是使用4个字节来表示数。但是前者需要涵盖小数,自然精度不如int。int能表示10位有效数字。

所以就是根据实际选择合适的数据类型。
2004-04-15 10:42:00 | [匿名:孙展波]
#回复: 1000.09009F.ToString("F5")得到1000.09000 编辑
float f = float.Parse("16867611");
int i = int.Parse("16867611");

结果:f=16867612,i=16867611
要float何用。。。。
2004-04-15 09:51:00 | [匿名:idiotzeng]
#回复: 1000.09009F.ToString("F5")得到1000.09000 编辑
多谢您的仔细,应该是C#的float和VB.NET的Single都是System.Single这个类型。
2004-04-15 08:35:00 | [匿名:孙展波]
#回复: 1000.09009F.ToString("F5")得到1000.09000 编辑
C#中Single
VB.NET中Float?
2004-04-15 08:09:00 | [匿名:test]
对不起,目前本随笔不允许发表新评论.

Powered by: Joycode.MVC引擎 0.5.2.0