Thread Rating:
  • 325 Vote(s) - 2.7 Average
  • 1
  • 2
  • 3
  • 4
  • 5
BigDecimal の丸め
11-20-2014, 11:19 AM,
#1
BigDecimal の丸め
BigDecimal の RoundingMode で毎回悩むので、メモ。

Code:
{def fr = {Frame}}
{def ary-rounding-mode =
    {{Array-of RoundingMode}
        RoundingMode.away-from-zero         || ゼロから遠いほうへ近づける
        , RoundingMode.ceiling              || 切り上げ(天井)
        , RoundingMode.floor                || 切り下げ(床)
        , RoundingMode.round-away-from-zero || 四捨五入
        , RoundingMode.round-toward-even    || 絶対値1以下五捨六入、絶対値1以上四捨五入?
        , RoundingMode.round-toward-zero    || 五捨六入
        , RoundingMode.truncate             || 小数点以下切り捨て
    }
}
{def test-data =
    {StringArray
        "-1.9", "-1.6", "-1.5", "-1.4", "-1.1",
        "-0.9", "-0.6",  "-0.5",  "-0.4", "-0.1" ,
        ||"0",
        "0.1","0.4", "0.5", "0.6", "0.9",
        "1.1", "1.4", "1.5", "1.6", "1.9"
    }
}
{def scale-tf = {TextField value = "0"}}
{def val-tf = {TextField value = "0.1"}}
{VBox
    {Table columns = 2, "スケール位置", scale-tf, "値", val-tf},
    {CommandButton
        label = "丸め結果一覧",
        {on Action do

            def scale = {scale-tf.value.to-int}
            def s = val-tf.value
            def tbl = {Table halign = "right", columns = 2 , "モード/元値", s}
            {for mode in ary-rounding-mode do
                {tbl.add mode}
                def bd = {BigDecimal.from-String s}
                def trun-bd = {bd.set-scale scale, rounding-mode = mode}
                {tbl.add {trun-bd.to-String}}
            }
            {popup-message tbl}
        }
    }
    ,{CommandButton
         label = "テストデータ:丸め結果一覧",
         {on Action do
             def scale = {scale-tf.value.to-int}
             def tbl = {Table halign = "right", columns =test-data.size + 1 , "モード/元値"}

             {for s in test-data do
                 {tbl.add s}
             }
             {for mode in ary-rounding-mode do
                 {tbl.add mode}
                 {for s in test-data do
                     def bd = {BigDecimal.from-String s}
                     def trun-bd = {bd.set-scale scale, rounding-mode = mode}
                     {tbl.add {trun-bd.to-String}}
                 }
             }
             {fr.add replace? = true, tbl}
         }
    }
}

{value fr}


Messages In This Thread
BigDecimal の丸め - by umemura - 11-20-2014, 11:19 AM

Possibly Related Threads...
Thread Author Replies Views Last Post
  BigDecimal -> int kay 1 4,309 03-09-2015, 11:35 AM
Last Post: umemura
  BigDecimal.to-String で、小数点桁に0が増えてしまう umemura 1 8,285 12-19-2013, 02:03 PM
Last Post: umemura
  なぜ String から BigDecimal にキャスト可能なのか umemura 2 4,475 12-12-2013, 01:24 PM
Last Post: crispincross
Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('1188')