javascript - Zapier - Javascript错误的自定义函数行为
问题描述
我在我的 Zapier 应用程序中定义了一个函数 numberFormat
我期待一个结果,50
但实际上我得到了5000
。关于如何解决这个问题的任何建议?
function numberFormat(number) {
var newString = number.toString().replace(/,/g, '.'); // togli tutte le virgole e sostituiscile con punti
var lastDotIndex = newString.lastIndexOf('.'); // l'ultimo che trovi è separatore dei decimali
var decimal = newString.slice(lastDotIndex); // parte decimale
var integerSlice = newString.slice(0, lastDotIndex); // parte intera
var integer = integerSlice.replace(/[^0-9]/g, ''); // della parte intera mi servono solo i numeri
var strResult = integer + decimal; // concatena intero e decimale
return Number(strResult);
}
// In this same environment I sent a test value like this:
console.log(numberFormat("50,00"));
解决方案
您需要添加一个点或只是替换并从中获取一个数字。
function numberFormat(number) {
return +number
.toString()
.replace(/,/g, '.')
.replace(/\.(?=.*\.)/g, '')
.replace(/[^0-9.]/g, ''); // allow dot
}
console.log(numberFormat("50,00"));
console.log(numberFormat("1,000,00"))
推荐阅读
- html - Node-RED 如何在模板 HTML 中接收 msg.payload
- excel - 计算日期差异
- php - 当查询包含双引号并出现在超链接内时,PHP 不显示查询的值
- android - 如何实现android的垂直滑动视图UI
- ispconfig - 在 AWS lightail 中全新安装后,ISPConfig 在端口 8080 中不起作用
- javascript - 时刻:只要当前时间和日期在请求时间之前最多 30 分钟,用户就应该能够执行 X
- javascript - mapbox 讲故事点击 div
- java - Spring应用程序Thymeleaf中的css问题
- c - 使用 %d 从 sscanf 获取错误输出
- angular - 在 Angular 8 / Spring Boot 应用程序中使用文本编辑器