Answers for "roman numerals to numbers"

5

Roman Numeral Converter

// Visit => https://duniya-roman-numeral-converter.netlify.app/
// Npm Package => https://www.npmjs.com/package/cr-numeral

const convertToRoman = num => {
    const numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    const roman = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
    let romanNumeral = ''

    // While num is not equal to 0, keep iterating with the value
    while(num !== 0){
      	// Find from the numbers array the match for the current number
        const index = numbers.findIndex(nums => num >= nums)
        
        // Keeping pushing the roman value to romanNumeral
        // Cause the found number from numbers matches the index of its
        // Corresponding roman below
        romanNumeral += roman[index]
      
      	// Set num to a new value by Substracting the used number from it 
        num -= numbers[index]
    }

    return romanNumeral
}

convertToRoman(3999);

// With love @kouqhar
Posted by: Guest on February-27-2021
0

roman numeral to numbers

/*
	// 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!!!"
Posted by: Guest on November-04-2021
0

roman numeral conversions

// Roman numeral conversions
fn int_to_roman(num: i32) -> String {
    let m = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
    let s = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];

    let (mut num, mut buf) = (num, vec![]);
    for i in 0..13 {
        let mut j = num / m[i];
        num %= m[i];
        while j > 0 {
            buf.push(s[i]);
            j -= 1;
        }
    }
    buf.into_iter().collect()
}

fn roman_to_int1(s: String) -> i32 {
    s.chars().rev().fold((0, 0), |(sum, prev), c| {
            let n = match c {
                'I' => 1,
                'V' => 5,
                'X' => 10,
                'L' => 50,
                'C' => 100,
                'D' => 500,
                'M' => 1000,
                _ => panic!("Not a roman numeral")};
            if n >= prev {              
                (sum + n, n)
            } else {
                (sum - n, n)
            }
        }).0
}

fn main() {
    let i = 2021;
    println!("Int {} to Roman {} to Int {}", i, int_to_roman(i), roman_to_int1(int_to_roman(i)));
}
Posted by: Guest on August-25-2021
0

roman numerals converter table embed

const inputField = document.querySelector('input'); // input element
const convertButton = document.getElementById('convert'); // convert button
const outputField = document.getElementById('display'); // output element
Posted by: Guest on August-25-2020
0

roman numbers to numbers

//Java Implementation of Roman To Number


public class RomanToNumber {
	public static int declareIntOfChar(char c)
	{
		int val=0;
		switch(c)
		{
		case 'I':
			val=1;
			break;
		case 'V':
			val=5;
			break;
		case 'X':
			val=10;
			break;
		case 'L' : 
			val=50;
			break;
		case 'C' : 
			val=100;
			break;
		case 'D' : 
			val=500;
			break;
		case 'M' : 
			val=1000;
			break;
		default :
			val=-1;	
			break;
		}
		return val;
	}
	public static void main(String[] args) {
		String s = "XCV";
		int sum = 0,c1,c2;
		for(int i=0;i<s.length();i++)
		{
			c1=declareIntOfChar(s.charAt(i));
			if(i+1<s.length())
			{
				c2=declareIntOfChar(s.charAt(i+1));
				if(c1<c2)
				{
					sum = sum + c2 - c1;
					i++;
				}
				else
				{
					sum = sum + c1;
				}
			}
			else
			{
				sum = sum + c1;
			}
		}
		System.out.print(s + " = " + sum);
	}
}
Posted by: Guest on January-30-2021

Code answers related to "roman numerals to numbers"

Browse Popular Code Answers by Language