Skip to content

距离单位

公里/海里转换

点我查看代码
vue
<script lang="ts" setup>
import { kmToNauticalMiles, nauticalMilesToKm } from '@summeruse/ol'
import { NFormItem, NInputNumber } from 'naive-ui'
import { computed, ref } from 'vue'

const km = ref(10)
const nauticalMiles = computed(() => kmToNauticalMiles(km.value).toFixed(2))

const nauticalMiles2 = ref(5.4)
const km2 = computed(() => nauticalMilesToKm(nauticalMiles2.value).toFixed(2))
</script>

<template>
  <NFormItem label="公里转海里">
    <NInputNumber v-model:value="km" :min="0" /> 公里
    <span> = {{ nauticalMiles }} 海里 </span>
  </NFormItem>
  <NFormItem label="海里转公里">
    <NInputNumber v-model:value="nauticalMiles2" :min="0" /> 海里公里
    <span> = {{ km2 }} 海里 </span>
  </NFormItem>
</template>

源代码

点我查看代码
ts
import { ONE_NM } from '../../constants'

/** 公里转海里 */
export function kmToNauticalMiles(km: number) {
  return (km * 1000) / ONE_NM
}

/** 海里转公里 */
export function nauticalMilesToKm(nauticalMiles: number) {
  return (nauticalMiles * ONE_NM) / 1000
}

/** 格式化Rotation */
export function formatRotation(rotation: number): number {
  if (rotation < 0) {
    return formatRotation(rotation + 2 * Math.PI)
  }
  if (rotation > 2 * Math.PI) {
    return formatRotation(rotation - 2 * Math.PI)
  }
  return rotation
}

/** 格式化角度 */
export function formatAngle(angle: number): number {
  if (angle < 0) {
    return formatAngle(angle + 360)
  }
  if (angle >= 360) {
    return formatAngle(angle - 360)
  }
  return angle
}

Released under the ISC License.