欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現(xiàn)在線進制轉(zhuǎn)換功能

 更新時間:2025年04月15日 11:17:00   作者:勘察加熊人  
這篇文章主要介紹了vue實現(xiàn)在線進制轉(zhuǎn)換功能,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧

vue實現(xiàn)在線進制轉(zhuǎn)換

主要功能包括:

1.支持2-36進制之間的轉(zhuǎn)換。
2.支持整數(shù)和浮點數(shù)的轉(zhuǎn)換。
3.輸入驗證(雖然可能存在不嚴格的情況)。
4.錯誤提示。
5.結(jié)果展示,包括大寫字母。
6.用戶友好的界面,包括下拉菜單、輸入框、按鈕和結(jié)果區(qū)域。
7.小數(shù)部分處理,限制精度為10位。
8.即時轉(zhuǎn)換(通過按鈕觸發(fā),而非實時響應(yīng))。

效果圖:

step1:C:\Users\wangrusheng\PycharmProjects\untitled18\src\views\Home.vue

<template>
  <div class="converter-container">
    <h1>在線進制轉(zhuǎn)換</h1>
    <p class="description">支持在2~36進制之間進行任意轉(zhuǎn)換,支持浮點型</p>
    <div class="converter-wrapper">
      <div class="converter-row">
        <div class="select-group">
          <select v-model="fromBase" class="base-select">
            <option v-for="n in bases" :value="n">{{ n }}進制</option>
          </select>
        </div>
        <div class="input-group">
          <input
            type="text"
            v-model="inputNumber"
            placeholder="轉(zhuǎn)換數(shù)字"
            class="number-input"
          >
        </div>
      </div>
      <div class="converter-row">
        <div class="select-group">
          <select v-model="toBase" class="base-select">
            <option v-for="n in bases" :value="n">{{ n }}進制</option>
          </select>
        </div>
        <div class="result-group">
          <div class="result-display">{{ result }}</div>
        </div>
      </div>
    </div>
    <button @click="convert" class="convert-btn">立即轉(zhuǎn)換</button>
  </div>
</template>
<script setup>
import { ref, computed } from 'vue'
const fromBase = ref(16)
const toBase = ref(10)
const inputNumber = ref('3c')
const result = ref('')
const bases = Array.from({ length: 35 }, (_, i) => i + 2); // 生成 2 到 36 的進制數(shù)組
const convert = () => {
  try {
    // Handle empty input
    if (!inputNumber.value) {
      result.value = '';
      return;
    }
    // Check if the input number is valid for the selected base
    const isValid = /^[0-9a-z.]+$/i.test(inputNumber.value);
    if (!isValid) {
      result.value = '輸入包含無效字符';
      return;
    }
    // Separate integer and fractional parts
    const [integerPartStr, fractionalPartStr = ''] = inputNumber.value.split('.');
    // Convert integer part
    const integerPartDecimal = parseInt(integerPartStr, fromBase.value);
    if (isNaN(integerPartDecimal)) {
      result.value = '無效的輸入數(shù)字';
      return;
    }
    const integerPartResult = integerPartDecimal.toString(toBase.value).toUpperCase();
    // Convert fractional part if it exists
    let fractionalPartResult = '';
    if (fractionalPartStr) {
      let decimalFraction = 0;
      for (let i = 0; i < fractionalPartStr.length; i++) {
        const digit = parseInt(fractionalPartStr[i], fromBase.value);
        if (isNaN(digit) || digit >= fromBase.value) {
          result.value = '無效的小數(shù)部分';
          return;
        }
        decimalFraction += digit * Math.pow(fromBase.value, -(i + 1));
      }
      let tempFractionalResult = '';
      let tempDecimal = decimalFraction;
      for (let i = 0; i < 10; i++) { // Limit precision to 10 digits
        tempDecimal *= toBase.value;
        const integerPart = Math.floor(tempDecimal);
        tempFractionalResult += integerPart.toString(toBase.value).toUpperCase();
        tempDecimal -= integerPart;
        if (tempDecimal === 0) {
          break;
        }
      }
      fractionalPartResult = '.' + tempFractionalResult;
    }
    result.value = integerPartResult + fractionalPartResult;
  } catch (error) {
    result.value = '轉(zhuǎn)換出錯';
    console.error("Conversion error:", error);
  }
}
</script>
<style scoped>
.converter-container {
  max-width: 600px;
  margin: 20px auto;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
  text-align: center;
  color: #333;
  margin-bottom: 10px;
}
.description {
  text-align: center;
  color: #666;
  margin-bottom: 30px;
}
.converter-wrapper {
  margin: 20px 0;
}
.converter-row {
  display: flex;
  gap: 10px;
  margin-bottom: 15px;
}
.select-group, .input-group, .result-group {
  flex: 1;
}
.base-select, .number-input {
  width: 100%;
  padding: 12px;
  border: 1px solid #fff;
  border-radius: 4px;
  font-size: 16px;
}
.result-display {
  padding: 12px;
  background: #f8f9fa;
  border: 1px solid #eee;
  border-radius: 4px;
  min-height: 46px;
}
.convert-btn {
  width: 100%;
  padding: 12px;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.3s;
}
.convert-btn:hover {
  background: #0056b3;
}
</style>

到此這篇關(guān)于vue實現(xiàn)在線進制轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)vue在線進制轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 手把手教你如何將html模板資源轉(zhuǎn)為vuecli項目

    手把手教你如何將html模板資源轉(zhuǎn)為vuecli項目

    Vue可以直接集成html,Vue就是前端框架,使用Vue做前端開發(fā)效率非常高,下面這篇文章主要給大家介紹了關(guān)于如何將html模板資源轉(zhuǎn)為vuecli項目的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • antd的選擇框如何增加tab選中的方法示例

    antd的選擇框如何增加tab選中的方法示例

    這篇文章主要為大家介紹了antd的選擇框如何增加tab選中的方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • el-tree樹組件懶加載(后端上千條數(shù)據(jù)前端進行處理)

    el-tree樹組件懶加載(后端上千條數(shù)據(jù)前端進行處理)

    本文主要介紹了el-tree樹組件懶加載(后端上千條數(shù)據(jù)前端進行處理),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景

    Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景

    Vuex是一個專為Vue.js應(yīng)用程序開發(fā)的狀態(tài)管理模式,下面這篇文章主要給大家介紹了關(guān)于Vue中狀態(tài)管理器(vuex)詳解以及實際應(yīng)用場景的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • Vue源碼解析之數(shù)組變異的實現(xiàn)

    Vue源碼解析之數(shù)組變異的實現(xiàn)

    這篇文章主要介紹了Vue源碼解析之數(shù)組變異的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-12-12
  • 使用idea創(chuàng)建第一個Vue項目

    使用idea創(chuàng)建第一個Vue項目

    最近在學習vue,本文主要介紹了使用idea創(chuàng)建第一個Vue項目,文中根據(jù)圖文介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 在vue中利用全局路由鉤子給url統(tǒng)一添加公共參數(shù)的例子

    在vue中利用全局路由鉤子給url統(tǒng)一添加公共參數(shù)的例子

    今天小編就為大家分享一篇在vue中利用全局路由鉤子給url統(tǒng)一添加公共參數(shù)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue之數(shù)據(jù)交互實例代碼

    vue之數(shù)據(jù)交互實例代碼

    本篇文章主要介紹了vue之數(shù)據(jù)交互實例代碼,vue中也存在像ajax和jsonp的數(shù)據(jù)交互,實現(xiàn)向服務(wù)器獲取數(shù)據(jù),有興趣的可以了解一下
    2017-06-06
  • 詳解vue組件之間相互傳值的方式

    詳解vue組件之間相互傳值的方式

    這篇文章主要介紹了vue組件之間相互傳值的方式,對vue感興趣的同學,可以參考下
    2021-05-05
  • vue3中reactive和ref函數(shù)及對比分析

    vue3中reactive和ref函數(shù)及對比分析

    這篇文章主要介紹了vue3中reactive和ref函數(shù)及對比,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-01-01

最新評論