【问题标题】:Format money data table value in c#在c#中格式化货币数据表值
【发布时间】:2017-02-02 01:12:59
【问题描述】:

任何人都可以帮助动态格式化两列的货币数据类型吗?

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Reflection;

namespace ST_367d705c54de4e9e9f890350f933c80b.csproj

{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
    #region VSTA generated code
    enum ScriptResults
    {
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    };
    #endregion


    public void Main()
    {
        string basePath = Dts.Variables["User::VarDirectoryPath"].Value.ToString();

        try
        {
            OleDbDataAdapter A = new OleDbDataAdapter();
            System.Data.DataTable dt = new System.Data.DataTable();

            var delimiter = Dts.Variables["User::VarDelimiter"].Value.ToString();
            A.Fill(dt, Dts.Variables["User::VarObject"].Value);

            basePath = Dts.Variables["User::VarDirectoryPath"].Value.ToString();
            string filePath = Path.Combine(basePath, (Dts.Variables["User::VarFileName_1"].Value.ToString()
                + Dts.Variables["User::VarDate"].Value.ToString() + Dts.Variables["User::VarFileExtension_1"].Value.ToString()));

            int i = 0;
            StreamWriter sw = null;

            using (sw = new StreamWriter(filePath, false))
            {
                for (i = 0; i < dt.Columns.Count; i++)
                {
                    sw.Write(dt.Columns[i].ToString() + Dts.Variables["User::VarDelimiter"].Value.ToString());
                }

                sw.WriteLine();

                foreach (DataRow row in dt.Rows)
                {
                    object[] array = row.ItemArray;
                    for (i = 0; i < array.Length; i++)
                    {
                        object val = array[i];
                        if (val == "0")
                            sw.Write(string.Format("#.##", val) + delimiter);
                        else
                            sw.Write(val + delimiter);
                    }

                    sw.WriteLine();
                }

                sw.Close();
            }
        }
        catch (Exception ex)
        {
            string file = string.Format("err_{0}.txt", Dts.Variables["User::VarDate"].Value.ToString());
            File.WriteAllText(Path.Combine(basePath, file), ex.ToString());
        }

        Dts.TaskResult = (int)ScriptResults.Success;
    }
}
}

有什么建议吗?数据表共有10列,第3、4、5列需要格式化为两位小数,如果值为0或null,还需要输入0.00。

【问题讨论】:

  • 这些列的数据类型是什么?您应该使用 float.tryparse 将其转换为浮点数或双精度数
  • 列的数据类型为十进制(18,2),在excel中默认为0。对于 null 或“0”值,我需要 0.00 的值。还需要如果值整数 529 那么它应该显示为 529.00
  • 不,我的意思是你的对象的数据类型是什么 val = array[i];对于那些列?调试和检查。您假设它们被解析为字符串,如果是这样,您需要在进行格式化之前将它们解析为双精度或浮点数。还要检查 string.isNullOrEmpty() 来处理空值。
  • 3,4 和 5 以十进制形式传递。
  • 那么你的代码到底出了什么问题呢?如果是十进制的话,我觉得很好......

标签: c# ssis dts c#-datatable


【解决方案1】:

这是一个格式化小数的例子:

https://dotnetfiddle.net/da7UoR

using System;

public class Program
{
    public static void Main()
    {
        decimal val1 = 0m;
        decimal val2 = 125580m;


        Console.WriteLine(val1.ToString("0.00"));
        //Output 0.00

        Console.WriteLine(val2.ToString("0.00"));
        //Output 125580.00

        //Money format
        Console.WriteLine(val1.ToString("C"));      
        //Output $0.00

        Console.WriteLine(decimalToFormattedString(val1));
        //Output 0.00

        Console.WriteLine(decimalToFormattedString(val2));
        //Output 125580.00
    }

    public static string decimalToFormattedString(decimal myValue)
    {   
        return myValue.ToString("0.00");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多