3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Power Automate for desktopの変数に関するTips「数値の切り捨て」アクションの挙動

Last updated at Posted at 2022-05-25

概要

Power Automate for desktopの変数アクション「数値の切り捨て」について調べました。ExcelのROUND関数と少し異なる挙動を取るため注意が必要です。

注意事項等

  • Power Automate for desktop 2.20.132.22123
  • 個人調べであることをご理解ください。
  • 2022年5月の情報です。
  • 現時点ではコピペで試せるサンプルフローを載せますがアップデート等で動作しなくなる可能性があります。
  • アップデートにあわせたサンプルフローの更新はしません。

試したこと

  • ExcelではROUND関数を使って0から10まで0.5刻みで小数点一位を四捨五入しました。
  • Power Automate for desktopでは「数値の切り捨て」アクションの「数値の四捨五入」操作を使って0から10まで0.5刻みで小数点一位を四捨五入しました。
  • 各数値を累計しました。

結果

Excel

image.png

Power Automate for desktop

image.png

「数値の切り捨て」アクション

「丸め」についてのルールは書かれていません。

Docs 数値の切り捨て

image.png

フロー

「数値の切り捨て」アクションの挙動をみるためにデータテーブルを生成します。変数内ですべて処理する方法およびPowerShellスクリプトアクションを使用した計算処理をしています。コメントに解説はかいています。リスト、データテーブル型変数を活用したサンプルになっています。コピペお試しいただければ幸いです。

スクリーンショット 2022-05-26 000735.jpg

Power Automate for desktop
/# ###############################
テーブル生成
################################/
SET Table TO { ^['元値', '四捨五入', '元値累計', '四捨五入値累計'] }
/# ###############################
初期値設定
################################/
SET RowCount TO 0
SET null TO $'''%''%'''
/# ###############################
0~10まで増分0.5でループ
################################/
LOOP LoopIndex FROM 0 TO 10 STEP 0.5
    /# ###############################
空リスト生成し[0]に元値[1]に小数点一位を四捨五入した値を書き込む
################################/
    SET List TO [null, null, null, null]
    SET List[0] TO LoopIndex
    Variables.TruncateNumber.RoundNumber Number: LoopIndex DecimalPlaces: 0 Result=> TruncatedValue
    SET List[1] TO TruncatedValue
    /# ###############################
テーブルにリストを行として追加する
################################/
    SET Table TO Table + List
    /# ###############################
元値四捨五入値の合計を[RowCount][2]および[3]に書き込む
################################/
    LOOP ColumnLoopIndex FROM 0 TO 1 STEP 1
        Variables.RetrieveDataTableColumnIntoList DataTable: Table ColumnNameOrIndex: ColumnLoopIndex ColumnAsList=> ColumnAsList
        Text.JoinText.JoinWithCustomDelimiter List: ColumnAsList CustomDelimiter: $''',''' Result=> JoinedText
        Scripting.RunPowershellScript Script: $'''$SumValue=(%JoinedText%)|Measure-Object -sum
$SumValue.sum''' ScriptOutput=> PowershellOutput ScriptError=> ScriptError
        SET Table[RowCount][ColumnLoopIndex + 2] TO PowershellOutput.Trimmed
    END
    Variables.IncreaseVariable Value: RowCount IncrementValue: 1 IncreasedValue=> RowCount
END

まとめ

Power Automate for desktopフロー内で計算する機会はすくないかもしれませんが、ExcelのRound関数と異なった挙動を確認できました。四捨五入について調べると結構深いです。というかそれぞれのルールが適用されているようなので(例えば銀行丸め)、なんでそうなるというより事実ベースで知っておくのは大切だと思います。最近は個人ブログの方への投稿しかしていませんが、意外な落とし穴になりそうなので変数シリーズとしてQiitaに書きました。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?