0

What my issue is that my client will enter the values in any other language. suppose marathi. i need to create bill according to input. what i have done is i took the input taken by user as string and assigned value to them this is working till 0-9 but what about 10, 100, 1000 and so on.. here is my code,plz suggest

function convert_number()
            {
                 var no1 = document.getElementById('rate').value
                if(no1 == "०")
                {
                    no1 = 0;
                    alert(no1);
                }
                else if(no1 == "१")
                {
                no1 = 1;
                alert(no1);             
                }
                else if(no1 == "२")
                {
                no1 = 2;
                    alert(no1);
                }
                else if(no1 == "३")
                {
                no1 = 3;
                    alert(no1);
                }
                else if(no1 == "४")
                {
                no1 = 4;
alert(no1);             
                }
                else if(no1 == "५")
                {
                no1 = 5;
alert(no1);             
                }
                else if(no1 == "६")
                {
                no1 = 6;
alert(no1);             
                }
                else if(no1 == "७")
                {
                no1 = 7;    
                alert(no1);
                }
                else if(no1 == "८")
                {
                no1 = 8;
alert(no1);             
                }
                else if(no1 == "९")
                {
                no1 = 9;
alert(no1);             
                }
                else
                {
                alert('invalid')    
                }

            }
0

3 Answers 3

1

You could get the UTF-16 code of the character, so you wouldn't need even a switch

var no1 = document.getElementById('rate').innerHTML

alert(no1.charCodeAt(0));
<p id="rate">r</p>

Sign up to request clarification or add additional context in comments.

6 Comments

so should i pass the number at str.charCodeAt(index) index parameter??
You give the position of the character, of a given string, for which you want the character code. For instance, by doing alert(no1.charCodeAt(0));, you get the UTF-16 numeric code of no1 at the first position. I've added a running example
it will return the ASCII value. but OP want to convert from one language to another. like Roman V-> 5
@SagarV So you say that he needs to map characters of multiple languages? By converting to UTF-16 value you get an integer, then you could map integers (characters) between them.
Just give a try on this "०".charCodeAt(0) in console. The required output is 0.
|
1

This code would convert numbers above and below 0-9:

var map = {"-":"-","०":0,"१":1,"२":2,"३":3,"४":4,"५":5,"६":6,"७":7,"८":8,"९":9};
function convert(str) {
  var converted_str = '';
  if (str.split('').find(char => {
    if (map.hasOwnProperty(char))
      converted_str += map[char];
    else
      return true;
    }) === true) {
    return NaN;
  } else {
    return parseInt(converted_str);
  }
}

Comments

0

This method will help in JavaScript.

Create a 2D array like this var nos=[['०',0],['१',1],['२',2]]; and add local digit in first index and original numbers in second index like I did.

Sample code included below
Try typing a marathi digit in the textbox and the click on the button. then check console

var nos=[['०',0],['१',1],['२',2],['३',3],['४',4],['५',5],['६',6],['७',7],['८',8],['९',9]];
function convert_number(num){
  if(typeof num!=typeof 123){
    var i=0;
    if(num.length>1)
      return convertmore(num);
    while(i<nos.length){
      if(nos[i][0]==num){
        return nos[i][1];
      }
      i++;
    }
 }
 else{
  return num;
 }
}
function convertmore(num){
  var i=num.split('');
  var result=0;
  i.forEach(function(x){
    result*=10;
    result+=convert_number(x);
  });
  return result;
}
<input id="num" type="text" />
<button onclick="console.log(convert_number(document.getElementById('num').value));">Convert</button>

3 Comments

sorry, but it's working only for single digit, i need to do it for 2 digit, 3 digit and 4 digit values
As I mentioned in the answer, it is a key. You have to extend the array
@manzuboy edited. now you can try any number. for example, try ४५

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.