1

I have a form, where user fills and Gujarati numeric numbers (in Gujarati), for eg. ૧૪ (which is 14). I want to make a check whether the entered Gujarati number is less than 50 (or ૫૦) or not.

How can i make this check in java and in javascript?

Ankit
  • 3,083
  • 7
  • 35
  • 59
  • 1
    Since Gujrati works on a decimal system, have you tried simply replacing the characters with the usual Arabic numerals and then performing the check? – Manishearth Dec 17 '13 at 09:20
  • @Manishearth I think he wants exactly the single-line doing this. – peterh Dec 17 '13 at 09:21
  • 1
    Found the solution on SOF only, http://stackoverflow.com/questions/8091373/locale-aware-number-conversion-in-javascript – Ankit Dec 17 '13 at 09:27

4 Answers4

4

You could convert it to an decimal and compare the result.

function gujaratiToDecimal (n) {
    return n.split ("").reduce (function (sum,chr,index,array) {
         var num = chr.charCodeAt (0) - 2790; //Subtract 2790 to get the decimal value for the current char
         num *= Math.pow (10, array.length - index - 1); //Multiply it with a power of ten, based on its position
         return sum + num //Sum it up
    },0)
}
gujaratiToDecimal ("૫૦"); //50
gujaratiToDecimal ("૧૪") < gujaratiToDecimal ("૫૦") //true

Here's a JSFiddle

I guess you could easily translate that to Java. -- Just installed an IDE, heres a Java version

 public int gujarati (String str) {
     int len = str.length ();
     int sum = 0;
     for (int i=0; i < len; i++) {
         sum += (str.charAt (i) - 2790 ) * Math.pow (10, len - i - 1);
     }
     return sum;
 }
Moritz Roessler
  • 8,542
  • 26
  • 51
1

You can fetch the uni-code values of characters entered. You can map those values with the values of 0-9 (e.g. ૧ maps to 1). Based on these, you can convert the number entered into Gujarati into one in English.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
1

I think this will work in java

public static int convertGujratiToEnglishNumber(String number) {
        int len = number.length();
        StringBuilder englishNumber = new StringBuilder();
        for (int i = 0; i < len; i++) {
            char c = number.charAt(i);

            switch (c) {
                case '१' :
                    englishNumber.append(1);
                    break;
                case '२' :
                    englishNumber.append(2);
                    break;

                case '३' :
                    englishNumber.append(3);
                    break;
                           ....
                           ..... until 9 or (९)
                default :
                    break;
            }
        }
        return Integer.parseInt(englishNumber.toString());
    }
Sunny
  • 14,522
  • 15
  • 84
  • 129
0

You can simply use a java script function which populates a hidden text value same as "gujrati" text box. So if I am a user and I enter ૧૪ , a hidden textbox should get populated with 14 and then you can compare it with 50. My quick thoughts there can be better opinions.

user1177969
  • 105
  • 8