Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8673813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:33:21+00:00 2026-06-12T19:33:21+00:00

I have to convert a German locale formatted String to a BigDecimal . However,

  • 0

I have to convert a German locale formatted String to a BigDecimal. However, I’m struggling with the best solution.

The following code shows my problem:

    String numberString = "2.105,88";

    NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
    try {
        Number parsed = nf.parse(numberString);
        BigDecimal bd1 = new BigDecimal(parsed.toString());
        System.out.println(bd1.toString());

        BigDecimal bd2 = new BigDecimal(parsed.doubleValue());
        System.out.println(bd2);

        BigDecimal bd3 = new BigDecimal(numberString);
        System.out.println(bd3);

    } catch (ParseException e) {
        e.printStackTrace();
    }

The outpout of this is

2105.88

2105.8800000000001091393642127513885498046875

Exception in thread “main” java.lang.NumberFormatException
at java.math.BigDecimal.(Unknown Source)
at java.math.BigDecimal.(Unknown Source)
at test.BigDecimalTest.main(BigDecimalTest.java:22)

The first output is correct, but it doesn’t really make sense to convert a String to a Number (Double to be precise), then back to a String again and then into a specific type of Number, BigDecimal.

The second output is incorrect, but could be solved by setting the scale of the BigDecimal. However, the amount of digits is not always known.

The third output is obviously not what I’m looking for.

My question: What would be the best way? Are there better ways to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:33:22+00:00Added an answer on June 12, 2026 at 7:33 pm

    It seems like there is no other way since java.Lang.Number doesn’t have a method which returns a BigDecimal type. Anyway it makes sense because BigDecimal only accepts strings which are properly formatted not like "2.105,88" but like "2105.88".

    Let me show your my code:

    import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import java.text.ParseException;
    import java.util.Locale;
    public class JavaMain {
        public static void main(String[] args) {
            String numberString = "2.105,88";
            //using casting
            try {
                DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN);
                df.setParseBigDecimal(true);
                BigDecimal bd = (BigDecimal) df.parseObject(numberString);
                System.out.println(bd.toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            //your way short version
            NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
            try {
                BigDecimal bd1 = new BigDecimal(nf.parse(numberString).toString());
                System.out.println(bd1.toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String numberStringFixed = "2105.88";
            //direct string formatted
            System.out.println(new BigDecimal(numberStringFixed));;     
            //direct but erroneous way if the string is not formatted
            System.out.println(new BigDecimal(numberString));;
            
        }
    }
    

    I hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to convert an incoming String field into a BigDecimal field that would
I have this weird problem that a convert of a string on my machine
I have to convert an C# code to VB.NET, but I've some problem....can you
I have to convert all my echo/print string from my php source code file
I have to convert a string into binary using UTF-8, but i am unable
I was wondering what the best approach would be to the following problem. Say
I am trying to convert an ISO 8601 formatted String to a java.util.Date .
I often have to convert a retreived value (usually as a string) - and
I have a problem i never faced before... I have to convert an hex
I recently have convert my project from VB6 to VB.NET2008, after the convert here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.