獲取小數點後2位數

 

轉:http://www.myexception.cn/asp-dotnet/89080.htmlhtml

 

double d=1.234; 
long a=(long)d; 
long temp=(long)((d-a)*100); 
------解決方案--------------------

Decimal Dec=Convert.ToDecimal(str)//將字符串轉爲Decimal型  
str=Decimal.Round(Dec,2).ToString() // 取小數點後2位,再轉爲字符串 

string類型沒有Round方法,因此要轉換類型 

------解決方案--------------------
1.只要求保留N位不四舍5入 

float f = 0.55555f; 
int i =(int)(f * 100); 
f = (float)(i*1.0)/100; 
2.保留N位,四捨五入 . 

decimal d= decimal.Round(decimal.Parse( "0.55555 "),2); 

3.保留N位四捨五入 

Math.Round(0.55555,2) 

4,保留N位四捨五入 
double dbdata = 0.55555; 
string str1 = dbdata.ToString( "f2 ");//fN 保留N位,四捨五入 

5.保留N位四捨五入 


string result = String.Format( "{0:N2} ", 0.55555);//2位 

string result = String.Format( "{0:N3} ", 0.55555);//3位 

6. 保留N位四捨五入 


double s=0.55555; 
result=s.ToString( "#0.00 ");//點後面幾個0就保留幾位 web

正則表達式保留小數點2位

function checkValue(currentElement, type)
{     
switch(type){ 
        case 'positiveInt': 
                       var pattern = /^[1-9]\d*$/;   
                       break;
        case 'decimal':   
                       var pattern = /^\d+\.?\d*$/;  
                       break;    
        case 'decimal_two':  
                       var pattern = /^\d+(?:\.\d{0,2})?$/;  
                       break;    
        default :        
     var pattern = /^[1-9]\d*$/;     }  
   if(!$(currentElement).val().match(pattern)){   
      $(currentElement).val('');     } }
相關文章
相關標籤/搜索