Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   How to convert a string to a number (http://www.velocityreviews.com/forums/t132406-how-to-convert-a-string-to-a-number.html)

 chirs 04-09-2004 04:46 AM

How to convert a string to a number

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

 Dave Miller 04-09-2004 05:49 AM

Re: How to convert a string to a number

In article <4c22a744.0404082046.7e540b5b@posting.google.com >,
yma@kicon.com 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

 Sudsy 04-09-2004 06:01 AM

Re: How to convert a string to a number

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?

 Roedy Green 04-09-2004 07:03 AM

Re: How to convert a string to a number

On 8 Apr 2004 21:46:11 -0700, yma@kicon.com (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.

 Roedy Green 04-09-2004 07:04 AM

Re: How to convert a string to a number

On Fri, 09 Apr 2004 02:01:47 -0400, Sudsy <bitbucket44@hotmail.com>
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.

 Sudsy 04-09-2004 02:51 PM

Re: How to convert a string to a number

>> 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!

 All times are GMT. The time now is 12:59 PM.