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?
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?
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;
}
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.
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());
}
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.