javascript如何分割得到经度小数点后6位?
How to split and get till 6 digit after the decimal of logitude in javascript?
我是 javascript 的新手,正在尝试使用经度坐标。
我有3个经度,我想将它们拆分并存储到小数点后6位
我一次只能做其中一个,这不太好,因为这些数字可以动态地来自用户输入。
例如:
let long1 = 151.21484501290186; // I want to store till 151.214845
let long2 = 77.55814612714227; // I want to store till 77.558146
let long3 = -122.0898574222976; // I want to store till -122.089857
// this method only works if starting value is of 2 digits but failed when it's 3
const result = long2.toString().substr(long2.toString().indexOf('.') - 2, 9);
console.log(result) // 77.558146
请帮助我。
要获得 6 位小数并保持为浮点数,您可以使用
并使用 parseFloat 转换回数字:
parseFloat(number.toFixed(6))
像这样
let longs = [151.21484501290186, // I want to store till 151.214845
77.55814612714227, // I want to store till 77.558146
-122.0898574222976 // I want to store till -122.089857
]
longs = longs.map(long => parseFloat(long.toFixed(6)))
console.log(longs)
// or just one of them
const long = 151.21484501290186;
const shorter = parseFloat(long.toFixed(6))
console.log(shorter)
最简单的方法就是使用 toFixed()
let long1 = 151.21484501290186; // I want to store till 151.214845
let long2 = 77.55814612714227; // I want to store till 77.558146
let long3 = -122.0898574222976; // I want to store till -122.089857
// this method only works if starting value is of 2 digits but failed when it's 3
const result = long2.toString().substr(long2.toString().indexOf('.') - 2, 9);
const result1 = long1.toFixed(6)
console.log(result) // 77.558146
console.log(result1)
我是 javascript 的新手,正在尝试使用经度坐标。
我有3个经度,我想将它们拆分并存储到小数点后6位
我一次只能做其中一个,这不太好,因为这些数字可以动态地来自用户输入。
例如:
let long1 = 151.21484501290186; // I want to store till 151.214845
let long2 = 77.55814612714227; // I want to store till 77.558146
let long3 = -122.0898574222976; // I want to store till -122.089857
// this method only works if starting value is of 2 digits but failed when it's 3
const result = long2.toString().substr(long2.toString().indexOf('.') - 2, 9);
console.log(result) // 77.558146
请帮助我。
要获得 6 位小数并保持为浮点数,您可以使用
并使用 parseFloat 转换回数字:
parseFloat(number.toFixed(6))
像这样
let longs = [151.21484501290186, // I want to store till 151.214845
77.55814612714227, // I want to store till 77.558146
-122.0898574222976 // I want to store till -122.089857
]
longs = longs.map(long => parseFloat(long.toFixed(6)))
console.log(longs)
// or just one of them
const long = 151.21484501290186;
const shorter = parseFloat(long.toFixed(6))
console.log(shorter)
最简单的方法就是使用 toFixed()
let long1 = 151.21484501290186; // I want to store till 151.214845
let long2 = 77.55814612714227; // I want to store till 77.558146
let long3 = -122.0898574222976; // I want to store till -122.089857
// this method only works if starting value is of 2 digits but failed when it's 3
const result = long2.toString().substr(long2.toString().indexOf('.') - 2, 9);
const result1 = long1.toFixed(6)
console.log(result) // 77.558146
console.log(result1)