现在有这样一组小数  0.2   18.35    0.26   12.342    0.05   
 我要转变成下面的格式  K0+200   K18+350   K0+260   K12+342   K0+50   
就是小数点前的不变,小数点后边的不足三位后边补零我在sql里实现 ('K'|| replace(CAST(F_NB_SFZZH AS VARCHAR(200)),'.','+')) as NF_NB_SFZZH ,这样只是把小数点改成了+,小数点前的0也没有了,请问这个怎么处理?

解决方案 »

  1.   

     'k'+ ltrim(str(numb))+'+'+substring(cast(numb as varchar(10)),charindex('.',numb)+1,len(numb))
      

  2.   

     'k'+ ltrim(str(numb))+'+'+substring(cast(cast(numb as decimal(8,3)) as varchar(10)),charindex('.',numb)+1,len(numb))
      

  3.   


    if object_id('tb') is not null
      drop table tb
    create table tb(xiaoshu decimal(10,3))
    insert tb
    select 0.2 union all
    select 18.35 union all
    select 0.26 union all
    select 12.342 union all
    select 0.05select 'K'+substring(Convert(varchar(8),xiaoshu),0,charindex('.',xiaoshu))+'+'+
    substring(Convert(varchar(8),xiaoshu),charindex('.',xiaoshu)+1,len(Convert(varchar(8),xiaoshu))-charindex('.',xiaoshu)) from tb/*
    K0+200
    K18+350
    K0+260
    K12+342
    K0+050
    */
      

  4.   

    这个是对的
     string tt=   x.Replace(x.Substring(0,x.IndexOf('.')),"K"+x.Substring(0,x.IndexOf('.')))+x.Replace(x.Substring(x.IndexOf('.'),"+"))+x.Replace(x.Substring(i+1,x.Length-1-x.IndexOf('.')));我中是用sql