//根據幣別控制金額小數位數
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)
*/

4129

被折叠的 条评论
为什么被折叠?



