【问题标题】:Format textField value with comma - Titanium用逗号格式化文本字段值 - Titanium
【发布时间】:2016-08-23 09:55:42
【问题描述】:

我有一个 textField 值为 12345678955。我想将此值格式化为 1,234,567.8955

想用逗号分隔值。

我已经厌倦了一些代码。但它不起作用。

【问题讨论】:

  • 把你的代码放在这里
  • 你想在ios还是android上做?
  • 在钛工作室。

标签: android ios titanium


【解决方案1】:

好吧,你想要得到你的小数点后 4 位,你需要将你的数字除以 10000:

var newNumber = parseInt($.yourTextField.value);
newNumber = Math.round(Number(newNumber)) / 10000;
console.log(newNumber); // 1234567.8955

接下来您要添加逗号:

var parts = newNumber.toString().split(".");
var num = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",") + (parts[1] ? "." + parts[1] : "");
console.log(num); // 1,234,567.8955

这就是功能,你如何将它与你的 textField 联系起来,以及通过哪个事件监听器,由你来解决。

(答案改编自 https://stackoverflow.com/a/25127753/829989,您可以自己轻松找到)

【讨论】:

  • 太好了...感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2014-05-31
  • 2020-12-18
  • 2017-12-09
  • 2011-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多