幣別控制金額小數位數方法

//根據幣別控制金額小數位數

private void DigitalControl()
{
// 控制項:設定單價UnitPrice,設定資本DealAmt,買進/賣出成本DealCost,應收付金額NetAmt
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
 
     if(this.lblDecLength.Text.Trim() !="")
     {
           int intDecLen = System.Convert.ToInt32(this.lblDecLength.Text.Trim());
           provider.NumberDecimalDigits = intDecLen;
           double dblDealAmt,dblDealCost,dblNetAmt; //dblUnitPrice
//         if (this.txtUnitPrice.Text.Trim() !="")    // 設定單價
//         {
//              dblUnitPrice =System.Convert.ToDouble(this.txtUnitPrice.Text.Trim());
//         }
//         else
//         {
//              dblUnitPrice = 0;
//         }
//          txtUnitPrice.Text = dblUnitPrice.ToString("N", provider);
                          
           if(this.txtDealAmt.Text.Trim() !="") // 設定資本
           {
                dblDealAmt =System.Convert.ToDouble(this.txtDealAmt.Text.Trim());
           }
           else
           {
                dblDealAmt = 0;
           }
           txtDealAmt.Text = dblDealAmt.ToString("N", provider);
                          
           if(this.txtDealCost.Text.Trim() !="") // 買進/賣出成本
           {
                dblDealCost =System.Convert.ToDouble(this.txtDealCost.Text.Trim());
           }
           else
           {
                dblDealCost = 0;
           }
           txtDealCost.Text = dblDealCost.ToString("N", provider);   
                          
           if(this.txtNetAmt.Text.Trim() !="") // 應收付金額
           {
                dblNetAmt =System.Convert.ToDouble(this.txtNetAmt.Text.Trim());
           }
           else
           {
                dblNetAmt = 0;
           }
           txtNetAmt.Text = dblNetAmt.ToString("N", provider);
 
                     //this.txtUnitPrice.DecimalDigit=Convert.ToInt32(this.lblDecLength.Text.Trim());     // 控制設定單價的小數位數
                     this.txtDealAmt.DecimalDigit=Convert.ToInt32(this.lblDecLength.Text.Trim());    // 控制設定資本的小數位數
                     this.txtDealCost.DecimalDigit=Convert.ToInt32(this.lblDecLength.Text.Trim());   // 控制買進/賣出成本的小數位數
                     this.txtNetAmt.DecimalDigit=Convert.ToInt32(this.lblDecLength.Text.Trim());          // 控制應收付金額的小數位數
     }
}
總結:
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
 
provider.NumberDecimalDigits = 2;     // 小数点保留几位数 .
double result = (double) 123.00000 ;    // 一定要用 double 类型 .
string d = result.ToString(" N " , provider); //result:d=123.00
  
Reference:
1.控制百分數方法
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
 
provider.PercentDecimalDigits = 2;    // 小数点保留几位数 .
provider.PercentPositivePattern = 2; // 百分号出现在何处 .
double result = (double)1 / 3;   // 一定要用 double 类型 .
string d = result.ToString("P", provider); //result:d=%33.33
2.控制货币值中小數方法
System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo();
 
provider.CurrencyDecimalDigits= 2;    // 小数点保留几位数 .
double result = (double)1234;  
string d = result.ToString(" C " , provider); //result:d=1234.00
 
[C#]
using System;
using System.Globalization;
 
class NumberFormatInfoSample {
 
   public static void Main() {
 
      // Gets a NumberFormatInfo associated with the en-US culture.
      NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
      // Displays a negative value with the default number of decimal digits (2).
      Int64 myInt = -1234;
      Console.WriteLine( myInt.ToString( "C", nfi ) );
      // Displays the same value with four decimal digits.
      nfi.CurrencyDecimalDigits = 4;
      Console.WriteLine( myInt.ToString( "C", nfi ) );
 
   }
}
/* 
This code produces the following output.
($1,234.00)
($1,234.0000)
*/
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值