0

我一直在使用一个简单的小脚本将数字四舍五入到小数点后百分之一,但是我注意到当数字以零结尾时,它会四舍五入到下一个小数位。我需要保留这个小数位。

例如: 7.49908302 将舍入为 7.5 而不是 7.50。

我怎样才能用这个子程序保持百分之一?我应该尝试一些 Perl 或 Objective C,因为有人告诉我 Applescript 不是这种事情的最佳选择。

这是电话:

set finalAge514years to (roundThis(age514years, 2))

这是我的舍入子程序:

on roundThis(n, numDecimals)
    set x to 10 ^ numDecimals
    (((n * x) + 0.5) div 1) / x
end roundThis
4

3 回答 3

2

数字7.57.50完全相同,因此 Applescript 在显示数字时会截断任何不必要的信息。您真正要寻找的是如何在显示该信息时对其进行格式化。为此,您需要将数字转换为字符串,指定在转换过程中要查看的小数位数。

这种格式化数字的方法实际上被认为是基本子程序之一

round_truncate(7.49908302, 2)
--> "7.50"

on round_truncate(this_number, decimal_places)
    if decimal_places is 0 then
        set this_number to this_number + 0.5
        return number_to_text(this_number div 1)
    end if

    set the rounding_value to "5"
    repeat decimal_places times
        set the rounding_value to "0" & the rounding_value
    end repeat
    set the rounding_value to ("." & the rounding_value) as number

    set this_number to this_number + rounding_value

    set the mod_value to "1"
    repeat decimal_places - 1 times
        set the mod_value to "0" & the mod_value
    end repeat
    set the mod_value to ("." & the mod_value) as number

    set second_part to (this_number mod 1) div the mod_value
    if the length of (the second_part as text) is less than the decimal_places then
        repeat decimal_places - (the length of (the second_part as text)) times
            set second_part to ("0" & second_part) as string
        end repeat
    end if

    set first_part to this_number div 1
    set first_part to number_to_string(first_part)
    set this_number to (first_part & "." & second_part)

    return this_number
end round_truncate

on number_to_string(this_number)
    set this_number to this_number as string
    if this_number contains "E+" then
        set x to the offset of "." in this_number
        set y to the offset of "+" in this_number
        set z to the offset of "E" in this_number
        set the decimal_adjust to characters (y - (length of this_number)) thru ¬
            -1 of this_number as string as number
        if x is not 0 then
            set the first_part to characters 1 thru (x - 1) of this_number as string
        else
            set the first_part to ""
        end if
        set the second_part to characters (x + 1) thru (z - 1) of this_number as string
        set the converted_number to the first_part
        repeat with i from 1 to the decimal_adjust
            try
                set the converted_number to ¬
                    the converted_number & character i of the second_part
            on error
                set the converted_number to the converted_number & "0"
            end try
        end repeat
        return the converted_number
    else
        return this_number
    end if
end number_to_string
于 2014-08-06T15:44:55.333 回答
1

您特别要求使用 Perl 或 Objective-C 替代 Applescript。

因此,这是一个 Perl 解决方案:

use strict;
use warnings;

print round_this(7.49908302, 2), "\n";

sub round_this {
  my ($n, $decimals) = @_;
  sprintf '%.*f', $decimals, $n;
}

输出

7.50
于 2014-08-06T15:07:13.663 回答
0

好吧,AppleScript 为您做了很多事情。因为通常 1.5 与布尔表达式中的 0.75 * 2 不同(或至少不稳定)。为了方便您进行真正的比较,1.50(0.75 * 2 的结果)将被强制转换为 1.5。要使其在 AppleScript 中工作,您需要实数的字符串表示形式。无论将一个实数强制转换为字符串,AppleScript 都会考虑系统的本地化。因此,机器之间的小数点和千位分隔符可能会有所不同,具体取决于其系统偏好。在将实数强制为整数时,AppleScript 会进行舍入。所以这样的事情可以为你工作:

set n to 7.49958302
set i to round n rounding toward zero
set f to (n - i) * 100 as integer
set f to text 1 thru 2 of (f & "00" as string)
set r to (i as string) & "." & f

当您需要将此字符串重新恢复为真实时,您可以轻松地使用运行脚本命令。运行脚本命令就像一个 eval。在 AppleScript 代码中,实数没有本地化。

run script "7.50"
于 2014-08-06T21:13:30.100 回答