zoukankan      html  css  js  c++  java
  • sqlserver把小数点后面多余的0去掉

    Sql中想把小数点后多余的0去掉,怎么办?

    select 5000/10000.0 --想变成0.5
    select 5500/10000.0 --想变成0.55
    select 5550/10000.0 --想变成0.555
    select 5555/10000.0 --想变成0.5555

    其结果分别为:0.5000000    0.5500000  0.5550000   0.5555000

    一、如果想去掉数字5后面多余的0 ,需要转化一下:

    select CONVERT(FLOAT,5000/10000.0) --想变成0.5
    select CONVERT(FLOAT,5500/10000.0) --想变成0.55
    select CONVERT(FLOAT,5550/10000.0) --想变成0.555
    select CONVERT(FLOAT,5555/10000.0) --想变成0.5555

    其结果分别为: 0.5  0.55  0.555 0.5555

    二、创建函数:

    在sql server 建个函数ClearZero,使用这个函数去掉小数点后面多余的零。

     CREATE function [dbo].[ClearZero](@inValue varchar(50))
    returns varchar(50)
    as
    begin
    declare @returnValue varchar(20)
    if(@inValue='')
       set @returnValue='' --空的时候为空
    else if (charindex('.',@inValue) ='0')
       set @returnValue=@inValue --针对不含小数点的
    else if ( substring(reverse(@inValue),patindex('%[^0]%',reverse(@inValue)),1)='.')
              set @returnValue =left(@inValue,len(@inValue)-patindex('%[^0]%',reverse(@inValue))) --针对小数点后全是0的
          else
              set @returnValue =left(@inValue,len(@inValue)- patindex('%[^0]%.%',reverse(@inValue))+1) --其他任何情形
    return @returnValue
    end

    另: 在c#中呢?

    decimal d = 0.0500m;

    d.ToString("0.##")就出来了

    也可以这样 string.Format("{0:0.##}",d000)

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    《EffectiveJava中文第二版》 高清PDF下载
    《MoreEffectiveC++中文版》 pdf 下载
    《啊哈c语言》 高清 PDF 下载
  • 原文地址:https://www.cnblogs.com/lgx5/p/7642892.html
Copyright © 2011-2022 走看看