2

オブジェクトをシリアル化すると、double 値が -9.9999999999988987E-05 として出力されます。これを修正して、小数点以下 4 桁の数値を取得するにはどうすればよいですか?

public class DecisionBar
    {
    public DateTime bartime 
         { get; set; }
    public string frequency
             { get; set; }
    public bool HH7
            {get;set;}
    public bool crossover
            {get;set;}
    public double mfe
            {get;set;}
        public double mae
            {get;set;}
                public double currentprofitability
            {get;set;}
    public double entryPointLong
            {get;set;}
    public double entryPointShort
            {get;set;}
    public double exitStopFull
            {get;set;}
    public double exitStopPartial 
                {get;set;}
     [XmlAttribute]         
    public string EntryOrExit
                {get;set;}
//    public DecisionBar()
//          {
//          crossover =false;
//          }

    }

出力。

<DecisionBar>
    <bartime>2012-07-24T08:59:00</bartime>
    <frequency>1 MINUTES</frequency>
    <HH7>false</HH7>
    <crossover>false</crossover>
    <mfe>0.00019999999999997797</mfe>
    <mae>-9.9999999999988987E-05</mae>
    <currentprofitability>0</currentprofitability>
    <entryPointLong>0</entryPointLong>
    <entryPointShort>0</entryPointShort>
    <exitStopFull>0</exitStopFull>
    <exitStopPartial>0</exitStopPartial>
</DecisionBar>
4

3 に答える 3

3

get で値を丸めるため、シリアライゼーション中にシリアライザーが丸められた値を読み取ります。

double _mfe;
double _mae;
        public double mfe
        {
             get
             {
                return Math.Round((decimal)_mfe, 4, MidpointRounding.AwayFromZero)
             }
             set
             {
                 _mfe = value;
             }
        }

        public double mae
        {
             get
             {
                return Math.Round((decimal)_mae, 4, MidpointRounding.AwayFromZero)
             }
             set
             {
                 _mae= value;
             }
        }
于 2012-07-24T14:18:54.967 に答える
2

を使用してシリアル化する前に double を丸めることができます

mfe = Math.Round(mfe, 4, MidpointRounding.AwayFromZero) 

これを自動化するには、実際のプロパティを[XmlIgnore]属性でマークし、丸められた値を返す新しいプロパティを作成します。

プライベート フィールドとアクセサーを使用しないことをお勧めしget; set;ます。これは、ゲッターがシリアル化するときだけではなく、毎回プロパティを丸めるからです。

于 2012-07-24T14:16:03.233 に答える
0

double を 10 進数 4 でフォーマットできます

double number = 123.64612312313;
number = double.Parse(number.ToString("####0.0000"));
于 2012-07-24T14:18:13.030 に答える