在VB.Net中删除小数数据类型变量中的分数值 [英] Removing the fraction value in Decimal datatype variable in VB.Net
问题描述
你好,
我在VB.Net中遇到小数数据类型转换的小问题
我有一个声明为小数的变量
dim var as deciaml
var = 12341234123412 . 341
现在,我需要删除该分数值并将其转换为整数.我的意思是我想要的结果是12341234123412341(仅删除点,并保留点后的3位数字.)
即使我的值是var = 1234.1,我也希望结果为1234100(在点3位数字加到整数后).我知道我可以通过乘以1000来做到这一点,但我希望其他可能性也能做到这一点.
迫切需要帮助.你为什么不喜欢呢?
[更新]
以下代码
Dim v As 十进制 = 100 . 5
v = v * 1000
Dim s As String = v.ToString (" )
Console.WriteLine(s)
输出
100500
[/update]
CPallini 的答案1很好.
如果您想不乘以1000进行转换,那么我认为可以先使用F3
格式将数字转换为String
表示形式,然后将.
替换为""
,如下所示
Dim var as 十进制 = 1234 .1D Dim varString as String = var.ToString (" ).替换(" .", ") Console.WriteLine(varString) ' 输出 ' 1234100
除以下以外的所有其他解决方案乘以1000会比较慢,所以如果您需要做很多次,那是最好的方法.
hello,
I am having a small conversion issue for Decimal datatyoe in VB.Net
I am having a variable declared as decimal
dim var as deciaml
var= 12341234123412.341
now I need to remove this fraction value and convert it into a whole number. I mean the result I want is 12341234123412341 ( only dot is removed and the 3 digits after dot are retained.)
even if I have the value var= 1234.1, I want the result as 1234100 ( after dot 3 digits sholud come and added to whole number). I know that I can do this by multiplying with 1000 but I want other possibilitis to achive this.
need help urgently.. Thanks
I know nothing better than multiply by1000
. Why don''t you like that?
[update]
The following code
Dim v As Decimal = 100.5 v = v * 1000 Dim s As String = v.ToString("F0") Console.WriteLine(s)
Outputs
100500
[/update]
Answer 1 by CPallini is good.
In case if you want to convert without multiplying with 1000, then I think the number can be first converted toString
representation usingF3
format and then the.
may be replaced with""
as shown below
Dim var as Decimal = 1234.1D Dim varString as String = var.ToString("F3").Replace(".","") Console.WriteLine(varString) 'Output '1234100
All other solutions other than multiplying by 1000 are going to be much slower, so if you need to do this lots and lots of times, that''s your best method.
这篇关于在VB.Net中删除小数数据类型变量中的分数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!