需要小数四舍五入到最接近的整数

发布于 2025-01-10 00:05:32 字数 275 浏览 0 评论 0原文

我正在使用价格调整器脚本,但在运行脚本后,我还需要它使用以下逻辑四舍五入到最接近的整数:如果小数部分为 .25 及更高,则向上舍入,否则向下舍入。

示例:

  • $1,316.10 变为 $1,316.00
  • $1,126.28 变为 $1,127.00

有没有办法也只影响某种角色风格?

I'm using the price adjuster script, but after I run the script I need it to also round to the nearest integer with the following logic: If the fractional part is .25 and higher, round up, and otherwise round down.

Examples:

  • $1,316.10 to become $1,316.00
  • $1,126.28 to become $1,127.00

Is there a way to also only affect a certain character style as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小梨窩很甜 2025-01-17 00:05:32

试试这个:

var style_name = 'my_style';

// find all the prices
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "\\$[\\d,]+\\.\\d+";
var prices = app.activeDocument.findGrep();
if (prices.length == 0) { alert('Nothing was found'); exit() }

// loop through all the finds
var i = prices.length;
while(i--) {

    // skip if the price has another style name
    if (prices[i].appliedCharacterStyle.name != style_name) continue;

    // get the numbers
    var numbers = prices[i].contents.slice(1).replace(/,/g,'').split('.')
    var number_left  = numbers[0];
    var number_right = numbers[1];

    // change the numbers
    if (number_right >= 25) number_left++;
    var rounded_number = '
 + add_commas(number_left) + '.00';

    // replace the price with the rounded number
    prices[i].contents = rounded_number;
}

// function to convert: 12345678 --> "12,345,678"
function add_commas(num) {
    var arr = num.toString().split('');
    var new_arr = [];
    while (arr.length) {
        new_arr.unshift([arr.pop(),arr.pop(),arr.pop()].reverse().join(''));
    }
    return new_arr.join(',');
}

Try this:

var style_name = 'my_style';

// find all the prices
app.findGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "\\$[\\d,]+\\.\\d+";
var prices = app.activeDocument.findGrep();
if (prices.length == 0) { alert('Nothing was found'); exit() }

// loop through all the finds
var i = prices.length;
while(i--) {

    // skip if the price has another style name
    if (prices[i].appliedCharacterStyle.name != style_name) continue;

    // get the numbers
    var numbers = prices[i].contents.slice(1).replace(/,/g,'').split('.')
    var number_left  = numbers[0];
    var number_right = numbers[1];

    // change the numbers
    if (number_right >= 25) number_left++;
    var rounded_number = '
 + add_commas(number_left) + '.00';

    // replace the price with the rounded number
    prices[i].contents = rounded_number;
}

// function to convert: 12345678 --> "12,345,678"
function add_commas(num) {
    var arr = num.toString().split('');
    var new_arr = [];
    while (arr.length) {
        new_arr.unshift([arr.pop(),arr.pop(),arr.pop()].reverse().join(''));
    }
    return new_arr.join(',');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文