Intl.NumberFormat 是 JS 中對語言敏感的格式化數字類的構造器類,JS 的語法:
new Intl.NumberFormat([locales[, options]])
Intl.NumberFormat.call(this[, locales[, options]])
QML 中是沒有 Intl 的,但是 QML 提供了 locale 方法:
function format(number) {
return Number(number).toLocaleString(Qt.locale("en_US"));
}
下面是一段測試代碼:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 1000
height: 400
color: "white"
title: "Test format number"
function format(number) {
return Number(number).toLocaleString(Qt.locale("en_US"));
}
Component.onCompleted: {
console.log("ZDS==========", format(12.343))
console.log("ZDS==========", format(12.345))
}
}
輸出結果:
qml: ZDS========== 12.34
qml: ZDS========== 12.35
可以看到是符合預期的。