how can i convert number string to number?(without using parsint)
convert number string to number
Collapse
X
-
Tags: None
-
A fair and valid question - this is exactly what parseInt is for. Check out this link: http://www.exampledepot.com/egs/java...onvertNum.html for examples on all the parse... functions.Comment
-
Hi Korea,
If str is a number string then u can convert to number by
int i=Integer.value Of(str)
Thanks,
PrasantComment
-
thx u for your help but i am java starter and my teacher sayed me to write code to convert string to number for example :
0ne million there hundred ===== 1.003.000Comment
-
As far as I'm aware there are no standard classes to achieve this as valueOf() takes a string like "1003000" and will convert it to an int.
I'm not sure how to parse a proper numeric name (One million and three thousand) into an int.
I would be thinking of a series of loops and checks to parse the string. Interested to hear if anyone else has any ideas on it as it could be an interesting little project.Comment
-
It all depends on how many different ways you want to be able to capture how someone is going to spell something. Are they going to say thirtyfour, thirty-four, or thirty four. And then there's the question of typos. If you're going to need to capture different ways of typing the same thing, I'm thinking regular expressions. But, seeing as how this is a school assignment, they probably just need to follow a strict format. In which case they can just code specifically for that format.Comment
-
"""ITSSS MY CODE TO CONVERT NUMBER TO TEXT """""
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* java for convert num to word ;)
* saeed nejahadreza
*/
package test2;
class wtn
{
// az methode split estefade gardad
}
class Ntw{
private static final String[] tensNames = { "", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" };
private static final String[] numNames = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" };
private static String convertLessThan OneThousand(int number)
{
String out;
if (number % 100 < 20)
{
out = numNames[number % 100];
number /= 100;
}
else
{
out = numNames[number % 10];
number /= 10;
out = tensNames[number % 10] + out;
number /= 10;
}
if (number == 0)
return out;
return numNames[number] + " hundred" + out;
}
public static String convert(int number)
{
if (number == 0)
{
return "zero";
}
int millions=0;
int hundredThousand s=0;
int thousands=0;
String snumber =Integer.toStri ng(number);
if(number>=1000 0000)
{
millions = Integer.parseIn t(snumber.subst ring(0,3));
hundredThousand s = Integer.parseIn t(snumber.subst ring(3,6));
thousands = Integer.parseIn t(snumber.subst ring(6,9));
}
else if(number>=1000 00)
{
hundredThousand s = Integer.parseIn t(snumber.subst ring(0,3));
thousands = Integer.parseIn t(snumber.subst ring(3,6));
}
else if(number>=1000 0)
{
hundredThousand s = Integer.parseIn t(snumber.subst ring(0,2));
thousands = Integer.parseIn t(snumber.subst ring(2,5));
}
else if(number>=1000 )
{
hundredThousand s = Integer.parseIn t(snumber.subst ring(0,1));
thousands = Integer.parseIn t(snumber.subst ring(1,4));
}
else
{
thousands = Integer.parseIn t(snumber.subst ring(0,3));
}
String tradMillions;
switch(millions )
{
case 0: tradMillions="" ; break;
default: tradMillions=co nvertLessThanOn eThousand(milli ons) + " million ";
}
String result = tradMillions;
String tradHundredThou sands;
switch(hundredT housands)
{
case 0: tradHundredThou sands=""; break;
default: tradHundredThou sands=convertLe ssThanOneThousa nd(hundredThous ands) + " thousand ";
}
result = result + tradHundredThou sands;
String tradThousand;
tradThousand = convertLessThan OneThousand(tho usands);
result = result + tradThousand;
return result;
}
}
public class Main {
public static void main(String[] args) {
int num=9746537;
System.out.prin tln( Ntw.convert(num ));
}
}Comment
-
Comment