1

how to cast String with period and comma to int, like

String a "9.000,00"
int b = Integer.parseInt(a);

when I run this code, I get an error message : Exception in thread "main" java.lang.NumberFormatException: For input string: "9.000,00"

Okem
  • 395
  • 1
  • 4
  • 12

5 Answers5

4

If you want to get as result 900000 then simply remove all , and . and parse it with for instance with Integer.parseInt or Long.parseLong or maybe even better use BigInteger if number can be large.

String a = "9.000,00";
BigInteger bn = new BigInteger(a.replaceAll("[.,]", ""));
System.out.println(bn);

Output: 900000


But if you want to parse 9.000,00 into 9000 (where ,00 part is decimal fraction) then you can use NumberFormat with Locale.GERMANY which uses form similar to your input: 123.456,78

String a = "9.000,00";
NumberFormat format = NumberFormat.getInstance(Locale.GERMANY);
Number number = format.parse(a);
double value = number.doubleValue();
//or if you want int
int intValue = number.intValue();
System.out.println(value);
System.out.println(intValue);

Output:

9000.0
9000
Pshemo
  • 122,468
  • 25
  • 185
  • 269
3
final String a = "9.000,00";
final NumberFormat format = NumberFormat.getInstance(Locale.GERMAN); // Use German locale for number formats
final Number number = format.parse(a); // Parse the number
int i = number.intValue(); // Get the integer value

Reference

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 2
    `(int) Double.parseDouble("9.000,00")` throws a `NumberFormatException`. – Paul Boddington Mar 11 '15 at 16:46
  • @Tom Code is tested :) I can make it prettier, but this is just a PoC... I always use `final`. – m0skit0 Mar 11 '15 at 16:52
  • 2
    Then you should have notices the problem with `final a` and the reassignment :P. Btw: you don't have to replace the points (the thousand separator), just use a locale that uses them as the thousand separator, like GERMAN. – Tom Mar 11 '15 at 16:58
  • The problem with `Locale.FRANCE` is that it doesn't have a default grouping separator `System.out.println(((DecimalFormat) format).getDecimalFormatSymbols().getGroupingSeparator());`prints nothing for me. Other locales print the correct separator. – Tom Mar 11 '15 at 17:03
  • lots of `final` clutter – Steve Kuo Mar 11 '15 at 17:31
  • 1
    @SteveKuo Your opinion. Luckily there are others as well: http://programmers.stackexchange.com/questions/115690/why-declare-final-variables-inside-methods. – Tom Mar 11 '15 at 17:33
  • 1
    @Tom Thanks, fixed. Strange thing is in France they use the thousands separator as well (most of the Mediterranean world uses it this way AFAIK). – m0skit0 Mar 11 '15 at 19:13
  • 1
    @m0skit0 [It looks like a *very new* bug](http://bugs.java.com/view_bug.do?bug_id=4510618) :D. – Tom Mar 11 '15 at 19:30
  • 1
    @Tom I found out that `GERMAN` and `FRENCH` work fine, but `GERMANY` and `FRANCE` do not... Go figure why. – m0skit0 Mar 12 '15 at 14:37
2

To do that, you need to use java.text.NumberFormat and NumberFormat.getInstance(Locale.FRANCE) (or another compatible Locale)

import java.text.NumberFormat;
import java.util.Locale;
class Test {
    public static void main(String[] args) throws Exception {
        NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
        String a = "9.000,00";
        a = a.replaceAll("\\.", "");
        Number number = format.parse(a);
        double d = number.doubleValue();
        int c = (int) Math.floor(d);   
        System.out.println(c);
    }
}

prints 9000 as you want ( and now is an int ) !

If I print every intermediate step :

import java.text.NumberFormat;
 import java.util.*;
class test {

public static void main(String[] args) throws Exception {
   NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
   String a = "9.000,00";
   a = a.replaceAll("\\.", "");
   System.out.println(a); // prints 9000,00
   Number number = format.parse(a);
   System.out.println(number); // prints 9000
   double d = number.doubleValue();
   System.out.println(d); // prints 9000.0
   int c = (int) Math.floor(d);   
   System.out.println(c); // prints 9000
}
}

so if Okem you want 9000,00 as you're saying in your comment, you just need

a = a.replaceAll("\\.", "");
System.out.println(a);

which gives you an output of 9000,00

I hope that helps.

adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • 2
    ... well I doubt the OP wants 9 as the final output. – Alexis C. Mar 11 '15 at 16:54
  • I fixed it output is now 9000 and an int ! – adrCoder Mar 11 '15 at 17:07
  • 2
    .. a bit overkill when you can simply do `int b = NumberFormat.getNumberInstance(Locale.GERMAN).parse("9.000,00").intValue();` and by the way all the classes in the lang package are automatically imported so you don't need to specify the import. – Alexis C. Mar 11 '15 at 17:07
  • Thanks for the input, I am sure there are "shorter" ways to do that :) – adrCoder Mar 11 '15 at 17:11
  • and how to remove the back of the two zero numbers, so when I'm using 'currencyFormat.format(a);' it will be 9.000,00 not 900.000,00 , actually i'm using ID currency format. – Okem Mar 11 '15 at 17:17
  • 1
    @Okem Please use [edit](http://stackoverflow.com/posts/28992401/edit) option (placed under your question post) and describe what exactly are you trying to accomplish. – Pshemo Mar 11 '15 at 17:19
  • @Okem I don't understand what you're saying, the output of my code if I print every intermediate step is 9000,00 9000 9000.0 9000 ... I don't see any 900.000,00 there ! – adrCoder Mar 11 '15 at 17:20
1

Try this -

String a = "9.000,00";
a = a.replace(",","");
a = a.replace(".","");
int b = Integer.parseInt(a);
Raj Srivastava
  • 669
  • 8
  • 10
1

I think DecimalFormat.parse is the Java 7 API way to go:

String  a = "9.000,00";
DecimalFormat foo = new DecimalFormat();
Number bar = foo.parse(a, new ParsePosition(0));

After that, you go and be happy with the Number you just got.

If you want the answer to be 900000 (it doesn't make sense to me, but I'm replying to your question) and put that into an int go with:

int b = Integer.parseInt(a.replaceAll(",","").replaceAll("\\.",""));

as already outlined in the comments.

Ostap
  • 300
  • 1
  • 6