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

  • Home
  • SEARCH
  • 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 7786195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:22:38+00:00 2026-06-01T20:22:38+00:00

float a= (float) 1846.4; NumberFormat df = DecimalFormat.getInstance(); df.setMinimumFractionDigits(2); df.setMaximumFractionDigits(2); float b = Float.parseFloat(

  • 0
float a= (float) 1846.4;

NumberFormat df = DecimalFormat.getInstance();
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);

float b = Float.parseFloat( df.format(a));

What would be the best way to set decimal places for a float (I just want to set 2 decimal places of a float value which can be 2013.43452 or 2392.2) ? Now I am getting exception like Exception in thread "main" java.lang.NumberFormatException: For input string: "1,846.40".

  • 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-01T20:22:39+00:00Added an answer on June 1, 2026 at 8:22 pm

    I think the direct problem causing the NFE is the thousand separators in the string, not the number of decimal places. You have both a ',' and a '.' in the string, and parseFloat doesn’t like that.

    Note, however, that you can’t fix the number of decimal places in a float value – it is entirely a formatting issue. Specifically, trailing 0‘s have no significance in floating point values, thus will not be shown by default. You need to format the value according to your wishes at the point of output (when converting it into a displayable String e.g. by using DecimalFormat as you did above).

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

Sidebar

Related Questions

... float value = Float.parseFloat((String)model.getValueAt(e.getLastRow(), 1)); DecimalFormat dec = new DecimalFormat(#.###); model.setValueAt(dec.format(value), e.getLastRow(), 1);
I have a float datatype and it gets assigned to 10 decimal points. When
Is float really missing from HtmlTextWriterStyle? I rarely use Control.Style.Add(HtmlTextWriterStyle.Whatever, myval); to set styles
I heard of float (4byte), double (8byte), decimal (16byte). I'm pretty sure i heard
I have a float value set to NaN (seen in the Watch Window), but
I have a float: 1.2333333 How do I change it into a two decimal
Float sum = new Float(300); // always somehow calculated DecimalFormat df = new DecimalFormat(#.#);
the data-type 'float' displays decimal numbers. by default my compiler displays up-to 6 decimals.
float [] pt1x = {0.580f, 0.680f, 0.780f}; float [] pt1y = {1.128f, 1.228f, 1.328f};
I have a float like so 23.248500 . Is it possible for me to

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.