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

如何为Kendo Numeric TextBox设置带国家代码的USD货币格式?

解决Kendo NumericTextBox添加国家代码的美元格式化问题

核心问题原因

kendo.format 和 kendo.toString/控件format属性的参数逻辑完全不同:

  • kendo.format 是模板式格式化,支持{0:c2}这种占位符语法,所以能正确解析并拼接国家代码;
  • kendo.toString 和 NumericTextBox的format属性只接受纯数值格式说明符,不识别占位符结构,直接传"US{0:c2}"会被当成普通文本+错误格式标记,导致输出US{123.45:c2}。

解决方案

1. 用kendo.toString实现带国家代码的格式化

有两种可行方式:

  • 字符串拼接法:先格式化货币,再手动拼接国家代码
    const formatted = "US" + kendo.toString(123.45, "c2", "en-US");
    // 输出:US$123.45
    
  • 自定义格式串法:直接把国家代码和货币符号写进格式说明符里
    const formatted = kendo.toString(123.45, "US$#,##0.00", "en-US");
    // 输出:US$123.45
    

2. 给Kendo NumericTextBox设置带国家代码的格式

控件的format属性不支持占位符,所以需要用以下两种方式:

  • 直接设置自定义格式串
    $("#numericBox").kendoNumericTextBox({
      value: 123.45,
      format: "US$#,##0.00",
      decimals: 2,
      culture: "en-US"
    });
    
  • 通过事件自定义格式化与解析(适合需要动态调整或兼容多区域的场景)
    $("#numericBox").kendoNumericTextBox({
      value: 123.45,
      decimals: 2,
      culture: "en-US",
      // 自定义显示格式
      format: function(value) {
        return "US" + kendo.toString(value, "c2", "en-US");
      },
      // 解析用户输入时去掉国家代码
      parse: function(input) {
        return kendo.parseFloat(input.replace(/US\$/g, ""));
      }
    });
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 11:05:14