ラベル round の投稿を表示しています。 すべての投稿を表示
ラベル round の投稿を表示しています。 すべての投稿を表示

20260313

[AppleScript] round  数値の丸め 切り捨て 切り上げ 四捨五入 絶対値丸め 五捨五入


仕様リファレンス
AppleScript Language Guide LINK
round LINK

切り捨て rounding down
切り上げ rounding up
四捨五入 rounding as taught in school
絶対値丸め rounding toward zero
五捨五入 rounding to nearest ※デフォルト値
整数化する as integer もこの五捨五入になります

負の数(マイナス)を考えなくて良い場面では
五捨五入か 切り上げ 切り捨て で考えても問題無い
マイナスの数値が関連する場合は
0に向けて丸めるのか? 結果負の値が加減する方向を意識すると良いですね

as integer

【スクリプトエディタで開く】 |

as integer.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003
004#デフォルト値である
005#rounding to nearestで丸めます
006#基本四捨五入で0.5の場合のみ偶数に丸める
007
008
009#普通に四捨五入
010log 74.5678 as integer
011--> 75
012#.5の場合偶数に丸める
013log 74.5 as integer
014--> 74
015#.5の場合偶数に丸める
016log 73.5 as integer
017--> 74
018#普通に四捨五入
019log 74.4321 as integer
020--> 74
021
022#普通に四捨五入
023log -74.5678 as integer
024--> -74
025#.5の場合偶数に丸める
026log -74.5 as integer
027--> -74
028#.5の場合偶数に丸める
029log -73.5 as integer
030--> 74
031#普通に四捨五入
032log -74.4321 as integer
033--> -74
AppleScriptで生成しました

round ※指定無い場合はデフォルト値のrounding to nearest

【スクリプトエディタで開く】 |

round.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003
004#オプション指定無しの場合は
005#デフォルト値の『rounding to nearest』で丸める
006#通常はいわゆる四捨五入
007round 74.5678
008--> 75
009round 74.4321
010--> 74
011
012#いわゆる四捨五入 .5以上なら負が増える .4なら負が減る
013round -74.5678
014--> -75
015round -74.4321
016--> -74
017
018#.5の場合のみ偶数に丸める
019round 74.5
020--> 74
021round -74.5
022--> -74
023
024round 73.5
025--> 74
026round -73.5
027--> -74
AppleScriptで生成しました

rounding to nearest

【スクリプトエディタで開く】 |

rounding to nearest.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003
004#これがデフォルト値
005#通常はいわゆる四捨五入
006round 74.5678 rounding to nearest
007--> 75
008round 74.4321 rounding to nearest
009--> 74
010
011#いわゆる四捨五入 .5以上なら負が増える .4なら負が減る
012round -74.5678 rounding to nearest
013--> -75
014round -74.4321 rounding to nearest
015--> -74
016
017#.5の場合のみ偶数に丸める
018round 74.5 rounding to nearest
019--> 74
020round -74.5 rounding to nearest
021--> -74
022
023round 73.5 rounding to nearest
024--> 74
025round -73.5 rounding to nearest
026--> -74
AppleScriptで生成しました

rounding down

【スクリプトエディタで開く】 |

rounding down.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003
004
005#普通に切り捨て
006round 74.5678 rounding down
007--> 74
008round 74.4321 rounding down
009--> 74
010
011#負数の場合は
012#0ゼロに対してdown=減るので
013#結果として負の値としては増える
014round -74.5678 rounding down
015--> -75
016round -74.4321 rounding down
017--> -75
AppleScriptで生成しました

rounding up

【スクリプトエディタで開く】 |

rounding up.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003
004#普通に切り上げ
005round 74.5678 rounding up
006--> 75
007round 74.4321 rounding up
008--> 75
009
010#負数の場合は
011#0ゼロに対してup =増やすので
012#結果として負の値としては減る
013round -74.5678 rounding up
014--> -74
015round -74.4321 rounding up
016--> -74
AppleScriptで生成しました

rounding as taught in school

【スクリプトエディタで開く】 |

rounding as taught in school.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003
004#いわゆる四捨五入
005round 74.5678 rounding as taught in school
006--> 75
007round 74.4321 rounding as taught in school
008--> 74
009
010#いわゆる四捨五入 .5なら負が増える .4なら負が減る
011round -74.5678 rounding as taught in school
012--> -75
013round -74.4321 rounding as taught in school
014--> -74
AppleScriptで生成しました

rounding toward zero

【スクリプトエディタで開く】 |

rounding toward zero.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003
004
005#いわゆる切り捨てと同様の処理
006#0ゼロに近い方へ切り捨て
007round 74.5678 rounding toward zero
008--> 74
009round 74.4321 rounding toward zero
010--> 74
011
012#負数の場合も
013#0ゼロに近い方へ切り捨て
014round -74.5678 rounding toward zero
015--> -74
016round -74.4321 rounding toward zero
017--> -74
AppleScriptで生成しました