[英]Count the number of digits before and after decimal places using Javascript
我有一個要求,我應該允許小數點前最多 14 位數字和小數點后最多 4 位數字。
有沒有一種方法可以讓用戶知道他是否輸入了 222222222222222.222 - 一旦他使用 Javascript 離開該文本框,小數點前 15 位將無效。
我試過這個,但它沒有幫助我:
MynewTextBox.Attributes.Add("onkeyup", "javascript:this.value=Comma(this.value);");
function Comma( Num ) {
var period = Num.indexOf('.');
if ( Num.length > (period + 4))
alert("too many after decimal point");
if ( period != -1 )
{
Num += '00000';
Num = Num.substr( 0, (period + 4));
}
另外,上面的 function 給了我錯誤:
預計 Object。
誰能幫我這個。
為什么不使用split()方法(下面的未經測試的代碼):
function Comma(num) {
var s = num.split('.');
if (s[0].length > 14) {
// Too many numbers before decimal.
}
if (s[1].length > 4) {
// Too many numbers after decimal.
}
}
編輯
以下將采用任何數字並返回一個小數點前最多 14 位和之后最多 4 位的數字(實際上它並沒有驗證輸入是一個數字,但你得到了圖片):
function Comma(num) {
var s = num.split('.');
var beforeDecimal = s[0]; // This is the number BEFORE the decimal.
var afterDecimal = '0000'; // Default value for digits after decimal
if (s.length > 1) // Check that there indeed is a decimal separator.
afterDecimal = s[1]; // This is the number AFTER the decimal.
if (beforeDecimal.length > 14) {
// Too many numbers before decimal.
// Get the first 14 digits and discard the rest.
beforeDecimal = beforeDecimal.substring(0, 14);
}
if (afterDecimal.length > 4) {
// Too many numbers after decimal.
// Get the first 4 digits and discard the rest.
afterDecimal = afterDecimal.substring(0, 4);
}
// Return the new number with at most 14 digits before the decimal
// and at most 4 after.
return beforeDecimal + "." + afterDecimal;
}
(和往常一樣,代碼未經測試。)
使用正則表達式
總結喜歡
pattern = /^\d{1,14)(\.{1,4}\)?$/;
if (patten.test(yourNumber)) {
// Hunky dory
}
else
{
// have another bash
}
我認為我們可以同意將數字轉換為字符串並計算小數點左邊的數字是粗略的,特別是考慮到非常大的數字可以轉換為科學記數法。
我不是計算機科學家,但這是我在 JS 中拼湊起來的東西,可以在數學上做到這一點。 支持負數、科學計數法,可以解析大到 10^308 小到 10^-323 的值。
function countDigits(value) {
if (value === 0) return { wholePlaces: 0, decimalPlaces: 0 };
var absValue = Math.abs(value); // -15.555 becomes 15.555
var wholePlaces = 0;
for (; wholePlaces <= 308; ++wholePlaces) { // Number.MAX_VALUE is 1.798e+308
if (absValue < Math.pow(10, wholePlaces))
break;
}
var decimalValue = absValue - Math.floor(absValue); // 15.555 - 15 = 0.555
var decimalPlaces = 0;
for (; decimalPlaces >= -323; --decimalPlaces) { // Number.MIN_VALUE is 5e-324
var temp = (decimalValue / Math.pow(10, decimalPlaces)) + 0.09; // Adding 0.09 to counter float errors
if (temp - Math.floor(temp) < 0.1) // If the decimal remaining is smaller that 0.1, we've reached the end
break;
}
decimalPlaces = Math.abs(decimalPlaces);
return {
wholePlaces,
decimalPlaces,
}
}
countDigits(0); // { wholePlaces: 0, decimalPlaces: 0 }
countDigits(0.10); // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(-0.10); // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(0.10000); // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(-0.10000); // { wholePlaces: 0, decimalPlaces: 1 }
countDigits(5); // { wholePlaces: 1, decimalPlaces: 0 }
countDigits(-5); // { wholePlaces: 1, decimalPlaces: 0 }
countDigits(15.555); // { wholePlaces: 2, decimalPlaces: 3 }
countDigits(-15.555); // { wholePlaces: 2, decimalPlaces: 3 }
countDigits(215.555); // { wholePlaces: 3, decimalPlaces: 3 }
countDigits(-215.555); // { wholePlaces: 3, decimalPlaces: 3 }
countDigits(1.55555e+4) // { wholePlaces: 5, decimalPlaces: 1 } (15555.5)
通過數學的另一種選擇
let digitsBefore = Math.ceil(Math.log10(Math.floor(Math.abs(x))+1))
其中 x 是數字digitsBefore+1+4
。 +1 是小數點,四是您允許的小數位數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.