如何在VB.NET中控制小数位 [英] How to control decimal places in VB.NET

查看:554
本文介绍了如何在VB.NET中控制小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何控制vb.net中的小数位?

例23.8008到23.8



我尝试了什么:



将a,b视为双倍

a = 23.8008

'如何将23.8008转换为23.8

How to control decimal places in vb.net?
Example 23.8008 to 23.8

What I have tried:

Dim a,b as double
a=23.8008
'How to convert 23.8008 to 23.8

推荐答案

打印您需要使用的值自定义数字格式字符串| Microsoft Docs [ ^ ]



例如
When printing the value you need to use Custom Numeric Format Strings | Microsoft Docs[^]

For example
Dim b As Double
b = 23.8008

System.Diagnostics.Debug.WriteLine(b.ToString("n1"))



第二个想法,如果你的意思是四舍五入,然后像


On the second thought, if you meant rounding, then something like

c = System.Math.Round(b, 1)


Quote:

如何控制vb.net中的小数位?

How to control decimal places in vb.net?



这取决于你的意思......如果你打算打印/显示它,请参阅解决方案#1 Wendelius [ ^ ]



但是,如果没有,我强烈建议您阅读:

每个计算机科学家应该知道的关于浮点运算的内容 [ ^ ]

< a href =http://blog.reverberate.org/2014/09/what-every-computer-programmer-should.html>浮点揭秘,第1部分 [ ^ ]


It depends on what you mean... If you mean to print/display it, see solution #1 by Wendelius[^]

But, if not, i's strongly recommend to read this:
What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]
Floating Point Demystified, Part 1[^]


这篇关于如何在VB.NET中控制小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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