格式化货币价值 [英] Format Money Value
本文介绍了格式化货币价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将货币数据类型的格式设置为"0.00",没有"$"标志.该值应始终使用两位小数格式.
I want to format the money data type to "0.00" without "$" sign. The value should always be formatted with two decimals.
任何帮助将不胜感激.
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;
}
}
}
-kccrga http://dbatrend.blogspot.com.au/
-kccrga http://dbatrend.blogspot.com.au/
推荐答案
Kccrga.
Hi Kccrga.
SQL Money数据类型映射到十进制数据类型.
SQL Money data type is mapped on decimal data type.
https://msdn.microsoft.com/it-it/library/cc716729(v = vs.110).aspx
然后可以将ToString方法与F2参数一起使用以显示2个十进制值.
Then you can use ToString method with F2 argument to display 2 decimal value.
decimal money = 10.6M;
Console.WriteLine(money.ToString("F2", CultureInfo.InvariantCulture));
这篇关于格式化货币价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文