Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to convert a string to a number

Reply
Thread Tools

How to convert a string to a number

 
 
chirs
Guest
Posts: n/a
 
      04-09-2004
Hi,

How to convert a string such as "1,000" to a number? I tried
Integer.parseInt("1,000");. But it did not work.

Thank you.

Chris
 
Reply With Quote
 
 
 
 
Dave Miller
Guest
Posts: n/a
 
      04-09-2004
In article < >,
says...
> Hi,
>
> How to convert a string such as "1,000" to a number? I tried
> Integer.parseInt("1,000");. But it did not work.
>
> Thank you.
>
> Chris
>

Look at java.text.NumberFormat - it can handle extraneous input like
commas, monetary symbols, etc.
--
Dave Miller
FundablePlans - Create a custom business plan online - only $39.95
http://www.fundableplans.com
 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      04-09-2004
chirs wrote:
> Hi,
>
> How to convert a string such as "1,000" to a number? I tried
> Integer.parseInt("1,000");. But it did not work.
>
> Thank you.
>
> Chris


Not surprising that it didn't work. Commas as part of the number don't
generally reflect how computers deal with numbers. But there is a class
designed to handle these situations. Try this:

import java.text.DecimalFormat;
....
DecimalFormat formatter = new DecimalFormat( "#,##0" );
String s = "1,000";
int i = -1;
try {
i = formatter.parse( s ).intValue();
}
catch( ParseException e ) {
...
}

Don't you just love the javadocs?

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-09-2004
On 8 Apr 2004 21:46:11 -0700, (chirs) wrote or quoted :

>How to convert a string such as "1,000" to a number? I tried
>Integer.parseInt("1,000");. But it did not work.


It does not like the comma. You will have to filter it out before
hand.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-09-2004
On Fri, 09 Apr 2004 02:01:47 -0400, Sudsy <>
wrote or quoted :

> DecimalFormat formatter = new DecimalFormat( "#,##0" );


When you do that, what sorts of string are acceptable. Does it insist
on the comma?

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      04-09-2004
Roedy asks:
>> DecimalFormat formatter = new DecimalFormat( "#,##0" );

>
> When you do that, what sorts of string are acceptable. Does it insist
> on the comma?


It's actually very forgiving. It seems to completely ignore
the commas, the number of digits between them, etc. I wrote
this quick test:

public class NumberTest {
public static void main( String args[] ) {
DecimalFormat formatter = new DecimalFormat( "#,##0" );
int parsed;
String formatted;
for( int i = 0; i < args.length; i++ ) {
try {
parsed = formatter.parse( args[i]
).intValue();
formatted = formatter.format( parsed );
System.out.println( "Original = " +
args[i] +
", parsed = " + parsed +
", formatted = " + formatted );
}
catch( Exception e ) {
System.err.println( e.toString() );
}
}
}
}

Here's a test run:

$ java NumberTest 1,000 1000 4,1567,890 4567890 1,34546
Original = 1,000, parsed = 1000, formatted = 1,000
Original = 1000, parsed = 1000, formatted = 1,000
Original = 4,1567,890, parsed = 41567890, formatted = 41,567,890
Original = 4567890, parsed = 4567890, formatted = 4,567,890
Original = 1,34546, parsed = 134546, formatted = 134,546
$

So it's more powerful on the formatting end than the parsing one.
Fair enough!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert string number to real number - ValueError: invalid literal davidj411 Python 11 05-29-2008 12:02 AM
convert decimal number in a hexadecimal number ? muss C Programming 13 03-27-2006 08:14 AM
How to convert a number to hex number? Hako Python 13 11-10-2005 01:56 AM
HELP - how to convert a string representation of a number to a number ? cpptutor2000@yahoo.com C++ 5 05-21-2005 05:56 PM
Convert decimal number in binary number makok VHDL 1 02-23-2004 06:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57