javascript truncate decimal without rounding

Solutions on MaxInterview for javascript truncate decimal without rounding by the best coders in the world

showing results for - "javascript truncate decimal without rounding"
Giorgia
14 Nov 2017
1// truncate function
2// in this example the default value is 2
3function truncate(number, index = 2) {
4  	// cutting the number
5    return +number.toString().slice(0, (number.toString().indexOf(".")) + (index + 1));
6}
7
8// example
9console.log(truncate(1234.56789, 3)); // 1234.567
10