C#中函数怎样去掉末尾的0和小数点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#中函数怎样去掉末尾的0和小数点相关的知识,希望对你有一定的参考价值。

参考技术A c#:
1:如果是textBox等控件值过滤0的话,可以使用TrimEnd('0')的方法;
eg:this.textBox1.Text.TrimEnd('0'),textBox1值为0.9900,则显示为0.99
2:使用ToString("g0")的方法
eg:0.99991000000000000.ToString("g0") 返回的值为0.99991
MSSQL中:
1:将小数用CAST函数转换
eg:select cast(0.2000 as real) 返回为 0.2
但是此方法常常因为转换的精度的问题,是数据与实际数据由差距
2:使用自定义函数转换
eg:

create function f_convert(@a decimal(20,8))
returns varchar(20)
as
begin
declare @re varchar(20),@r2 varchar(20),@i int
select @re=convert(varchar(20),@a)
,@i=charindex('.',@re)
,@r2=reverse(substring(@re,@i,20))
,@r2=reverse(substring(@r2,patindex('%[^0]%',@r2),20))
,@re=left(@re,@i-1)
if @r2<>'.' set @re=@re+@r2
return(@re)
end
go

--测试数据
declare @tb table(a decimal(20,8))
insert into @tb
select 0.20
union all select 0.21
union all select 0.21989

--查询
select a,转换后=dbo.f_convert(a) from @tb
go

--删除函数
drop function f_convert

/*--测试结果
a 转换后
---------------------- --------------------
.20000000 0.2
.21000000 0.21
.21989000 0.21989

(所影响的行数为 3 行)
--*/
参考技术B 哥们,如下

string floatValue = "23.000";
floatValue = floatValue.TrimEnd('.', '0');
Console.WriteLine(floatValue);本回答被提问者采纳
参考技术C string strTemp=1.0;
strTemp=strTemp.TrimEnd("0");
strTemp=strTemp.TrimEnd(".");
参考技术D 请说的具体点,比如什么样子的数据

Java有一个小数,如何去掉小数部分?

Java有一个小数,如何去掉小数部分?

1、Math.round(float f)对小数部分四舍五入
或者强值类型转换成int类型,直接去掉小数部分!

2、

3、向上取整:Math.ceil() //只要有小数都+1
向下取整:Math.floor() //不取小数
四舍五入:Math.round() //四舍五入

参考技术A Math.round(float f)对小数部分四舍五入
或者强值类型转换成int类型,直接去掉小数部分本回答被提问者和网友采纳
参考技术B Math.round(你的数);得到的就是去掉小数部分的数哈。。并且四舍五入了的哈。。 参考技术C 楼上的两个方法都对 你可以直接转换为int 这样就去掉了 参考技术D 假设你的数a=1.001

(int) a=1;

加我为满意答案吧

以上是关于C#中函数怎样去掉末尾的0和小数点的主要内容,如果未能解决你的问题,请参考以下文章

.net 如何去掉小数点后多余的0

小数末尾的零能去掉吗?为啥?

EXCEL中小数点后面的0怎么去掉

在表示近似数时,小数末尾的零能不能去掉?

js取整,保留小数位数、四舍五入、科学记数法及去掉数字末尾多余的0

sql 去掉小数点最后的零