在Oracle中自动格式化货币字段

时间:2015-05-24 04:37:57

标签: sql oracle

我正在开发Oracle报告。它有大约15个货币字段。派生这些值的数据库列的数据类型为NUMBER(m,n)。

我在报告中格式化它们如下:

    [TestMethod]
    public void Test_Person_Equals_with_ExpectedObjects()
    {
        //use extension method ToExpectedObject() from using ExpectedObjects namespace to project Person to ExpectedObject
        var expected = new Person
        {
            Id = 1,
            Name = "A",
            Age = 10,
        }.ToExpectedObject();

        var actual = new Person
        {
            Id = 1,
            Name = "A",
            Age = 10,
        };

        //use ShouldEqual to compare expected and actual instance, if they are not equal, it will throw a System.Exception and its message includes what properties were not match our expectation.
        expected.ShouldEqual(actual);
    }

    [TestMethod]
    public void Test_PersonCollection_Equals_with_ExpectedObjects()
    {
        //collection just invoke extension method: ToExpectedObject() to project Collection<Person> to ExpectedObject too
        var expected = new List<Person>
        {
            new Person { Id=1, Name="A",Age=10},
            new Person { Id=2, Name="B",Age=20},
            new Person { Id=3, Name="C",Age=30},
        }.ToExpectedObject();

        var actual = new List<Person>
        {
            new Person { Id=1, Name="A",Age=10},
            new Person { Id=2, Name="B",Age=20},
            new Person { Id=3, Name="C",Age=30},
        };

        expected.ShouldEqual(actual);
    }

    [TestMethod]
    public void Test_ComposedPerson_Equals_with_ExpectedObjects()
    {
        //ExpectedObject will compare each value of property recursively, so composed type also simply compare equals.
        var expected = new Person
        {
            Id = 1,
            Name = "A",
            Age = 10,
            Order = new Order { Id = 91, Price = 910 },
        }.ToExpectedObject();

        var actual = new Person
        {
            Id = 1,
            Name = "A",
            Age = 10,
            Order = new Order { Id = 91, Price = 910 },
        };

        expected.ShouldEqual(actual);
    }

    [TestMethod]
    public void Test_PartialCompare_Person_Equals_with_ExpectedObjects()
    {
        //when partial comparing, you need to use anonymous type too. Because only anonymous type can dynamic define only a few properties should be assign.
        var expected = new
        {
            Id = 1,
            Age = 10,
            Order = new { Id = 91 }, // composed type should be used anonymous type too, only compare properties. If you trace ExpectedObjects's source code, you will find it invoke config.IgnoreType() first.
        }.ToExpectedObject();

        var actual = new Person
        {
            Id = 1,
            Name = "B",
            Age = 10,
            Order = new Order { Id = 91, Price = 910 },
        };

        // partial comparing use ShouldMatch(), rather than ShouldEqual()
        expected.ShouldMatch(actual);
    }

Oracle是否有办法根据字段的数据类型精度自动格式化字段?

基本上我想阻止对SELECT TO_CHAR(field1,'99,999,999.99') --field1 datatype NUMBER(8,2) SELECT TO_CHAR(field2,'99,999,999,999.99') --field2 datatype NUMBER(11,2) 格式进行硬编码,并让Oracle这样做,因为它已经知道该字段的精确度。

1 个答案:

答案 0 :(得分:3)

我相信你在这里寻找的是格式面具。

SELECT TO_CHAR(99999999.99,'FM9G999G999G999G990D00') from dual;
Output: 99,999,999.99

SELECT TO_CHAR(99999999999.99,'FM9G999G999G999G990D00') from dual;
Output: 99,999,999,999.99

SELECT TO_CHAR(123.54,'FM9G999G999G999G990D00') from dual;
Output: 123.54

SELECT TO_CHAR(18945.65,'FM9G999G999G999G990D00') from dual;
Output: 18,945.65

编辑:澄清FM9G999G999G999G990应该处理最大精度,而D00规模到十分之一。