2017-02-28 36 views
2

假設我們有這樣一個字符串:2002。我想刪除最後一個字符並將結果的最後一個字符舍入。這意味着正確的輸出是:201Javascript刪除最後一個字符並向上取整

注意:如果最後一個字符是0,我們不需要四捨五入;

更多例如:

100 --- --- to:10

200 --- --- to:20

101 --- --- to:11

222 --- to: --- 23

30006 --- to: --- 3001

1299 --- to: --- 130

4089 --- to: --- 409

5099 --- to: --- 510

其實它是貨幣轉換阿曼里亞爾到託曼

+2

你的意思是你想除以十? – Ryan

+0

幾乎是這樣。 – mohammad

回答

5

鴻溝number十一個解決方案,並使用Math.ceil圍捕。

Math.ceil(number/10); 
1

var a=parseInt(prompt("enter a number")); 
 
var result=Math.ceil(a/10); 
 
alert(result);

+0

對於實際用法,parseInt()應該在'a'變量周圍,而不在'Math.ceil'結果的周圍。 – tilz0R

+0

隨時編輯帖子:) –

0

function doYourJob(number){ 
 
    var withoutLastChar = number.slice(0,-1); //remove last char 
 
    var toInteger = +withoutLastChar; //Convert to number 
 
    return toInteger + 1; //Add 1 
 
} 
 

 
console.log(doYourJob("2002")); 
 
console.log(doYourJob("222")); 
 
console.log(doYourJob("1299"));

+0

@Downvoter爲什麼? – Weedoze

+1

非常醜陋,爲什麼複雜? – tilz0R

+0

@ tilz0R Downvote,因爲它很醜? + OP說他有**字符串**而不是數字。當答案沒有幫助時應使用Downvote – Weedoze

1

var rounder = function(num){ 
 
    return Math.ceil(num/10); 
 
} 
 

 
console.log(rounder(100)); 
 
console.log(rounder(101));

相關問題