You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何拆分输入值并按阶梯计费规则计算对应输出金额

Tiered User Billing Calculation Implementation

Got it, let's tackle this tiered billing calculator step by step. I'll use JavaScript for the example since it’s perfect for web-based input/output interactions, but the core logic translates easily to any language you’re working with.

First, let’s restate the pricing rules clearly for reference:

  • Tier 1 (0–200 users): $4 per user. Total = users * 4
  • Tier 2 (201–500 users): First 200 at $4, remaining (users - 200) at $1. Total = (200 * 4) + ((users - 200) * 1)
  • Tier 3 (501–1000 users): First 200 at $4, next 300 (201–500) at $1, remaining (users - 500) at $0.50. Total = (200 * 4) + (300 * 1) + ((users - 500) * 0.5)

Step-by-Step Implementation Code

Here’s a complete function to handle the calculation, plus a simple HTML setup for your input/output boxes:

// Core calculation function
function calculateBilling() {
  // Grab input value and convert to a number
  const inputElement = document.getElementById('userCountInput');
  const userCount = parseInt(inputElement.value);
  
  // Validate input (ensure it's a positive integer within 0–1000)
  if (isNaN(userCount) || userCount < 0 || userCount > 1000) {
    document.getElementById('billingOutput').value = "Enter a valid number between 0 and 1000";
    return;
  }
  
  let totalCost = 0;
  
  // Apply tiered pricing logic
  if (userCount <= 200) {
    totalCost = userCount * 4;
  } else if (userCount <= 500) {
    totalCost = (200 * 4) + ((userCount - 200) * 1);
  } else { // 501–1000 users
    totalCost = (200 * 4) + (300 * 1) + ((userCount - 500) * 0.5);
  }
  
  // Format to 2 decimal places for proper currency display
  document.getElementById('billingOutput').value = `$${totalCost.toFixed(2)}`;
}

Corresponding HTML for Input/Output

<!-- User count input box -->
<input type="number" id="userCountInput" placeholder="Enter number of users" min="0" max="1000">
<!-- Button to trigger calculation -->
<button onclick="calculateBilling()">Calculate Billing</button>
<!-- Read-only output box for the result -->
<input type="text" id="billingOutput" readonly placeholder="Billing amount">

How It Works

  1. Input Validation: We first check if the input is a valid number within the allowed range to avoid broken calculations.
  2. Tiered Logic: Conditional statements apply the correct pricing tier. Each tier builds on the previous one, so we don’t waste time recalculating lower-tier costs.
  3. Currency Formatting: The result is formatted to two decimal places to match standard currency conventions.

Test Cases to Verify

  • Input 20 → Output $80.00 (matches your example)
  • Input 300 → Output $900.00 (2004 + 1001 = 800 + 100 = 900)
  • Input 555 → Output $1127.50 (2004=800, 3001=300, 55*0.5=27.5; total 1127.5)

内容的提问来源于stack exchange,提问作者Frunky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:04:54