📜  罗马数字转数字 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:13.290000             🧑  作者: Mango

代码示例1
/*
    // This application helps you convert roman numerals to numbers or vice-versa
*/

// First install the package @ "npm install cr-numeral"
// Then import or require the package in your application
const {
  convertNumberToRoman: cnr,
  convertRomanToNumber: crn,
} = require("cr-numeral");
// OR
const cnr = require("cr-numeral").convertNumberToRoman;
const crn = require("cr-numeral").convertRomanToNumber;

// Define your variables
const number = 2021;
const numeral = "MMMXXV"; // Case-insensitive

// Use your package/module
const toRoman = cnr(number);
const toNumber = crn(numeral);

// Log or use your result
console.log(toRoman, toNumber);

            // Converting a number to Roman Numeral
const { convertNumberToRoman } = require('cr-numeral');
// OR
const convertNumberToRoman = require('cr-numeral').convertNumberToRoman;

convertNumberToRoman(2021));
"MMXXI"

convertNumberToRoman(-2021)); // Can not convert a negative number or zero
"Can not convert Zero or negative numbers!!!"

convertNumberToRoman("na256m"));
"You must provide only valid numbers!!!"

convertNumberToRoman(false));
"Cannot use Boolean values!!!"

convertNumberToRoman(true));
"Cannot use Boolean values!!!"

            // Converting Roman Numeral to Number
const { convertRomanToNumber } = require('cr-numeral');
// OR
const convertRomanToNumber = require('cr-numeral').convertRomanToNumber;

convertRomanToNumber("MMXXI"));
"2021"

convertRomanToNumber("na256m"));
"Provide a valid roman character!!!"
"Cause these are invalid roman numerals : [ N,A,2,5,6 ]"

convertRomanToNumber(6355));
"You must provide only valid strings!!!"

convertRomanToNumber(false));
"Cannot use Boolean values!!!"

convertRomanToNumber(true));
"Cannot use Boolean values!!!"