gpt4 book ai didi

javascript - 如何获得用户偏好的单位?米或英里

转载 作者:行者123 更新时间:2023-11-29 11:28:46 25 4
gpt4 key购买 nike

我正在用 React Native 开发一个应用程序,我想与我的用户保持一定的距离。

因为我住在欧洲,所以我使用了“公里”系统,但我想知道我的用户对此指标的设置,以便我可以使用正确的单位显示值。

我已经使用过这个模块:https://github.com/react-native-community/react-native-device-info

但是好像没有这个功能。

你知道有哪些模块吗?或者链接一些原生函数的快速方法?

提前致谢

最佳答案

正如评论中已经建议的那样,您可以使用 react-native-device-info。请参阅下面的工作示例:

  transformDistance(distanceKM) {
const countryCode = DeviceInfo.getDeviceCountry(); // get country code
const factor = 0.621371; // constant scale factor: 1km = 0.621371 miles
if (countryCode == "US") {
return <Text> {distanceKM * factor} Miles </Text>;
}
// add other countries as additional if statements here
//

//otherwise return distance in metric unit
return <Text> {distanceKM} Km </Text>;
}

render() {
return (
<View style={styles.container}>
<View>
{this.transformDistance(5)} // outputs 3.106855 Miles or 5 Km
</View>
</View>
);
}

关于javascript - 如何获得用户偏好的单位?米或英里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55798328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com