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 6193235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:05:01+00:00 2026-05-24T03:05:01+00:00

In a web application ,I want to model an ItemForSale which has a price

  • 0

In a web application ,I want to model an ItemForSale which has a price field.I read elsewhere that float or double should not be used for currency fields due to rounding errors and BigDecimal is the proper type for this purpose.I created these classes

class ItemForSale {
    private String name;
    private BigDecimal price;
    ...
}

class MyUtils{
    ...
    public static BigDecimal parsePriceOfItem(String priceStr){
        BigDecimal price;
        BigDecimal zero = new BigDecimal(0);
        try{
            price = new BigDecimal(priceStr);
        }catch(NumberFormatException nfe){
            price = zero;
        }
        if(price.doubleValue() < zero.doubleValue()){
            price = zero;
        }
        return price;
        }
    }

Is this the right way to parse a price string(as entered by user)?I wanted to treat negative and invalid strings (say ‘abcd’) as 0.

If there is a better way ,please tell me

thanks

mark

  • 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-05-24T03:05:01+00:00Added an answer on May 24, 2026 at 3:05 am

    Why would you want to treat invalid input as 0? Surely you’d want to tell the user that they’ve made a mistake, rather than treating it as if they’d typed in zero.

    If you’re parsing user input, you should probably be using DecimalFormat instead of the BigDecimal constructor – that way it will use the appropriate cultural information. (Use setParseBigDecimal to make DecimalFormat parse to BigDecimal instead of double.)

    Then instead of converting the BigDecimal values to doubles, use:

    if (price.compareTo(BigDecimal.ZERO) < 0)
    

    I would suggest that you should indicate to the user three different states:

    • Number can’t be parsed
    • Number was negative (or possibly invalid in some other way; do you have a maximum value, or a maximum number of digits)?
    • Number was valid
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my spring web application I have arraylist in model attribute which I want
I want to create a web application with a model that persists between HTTP
I have a .Net web application that I want to take a copy of
I have a web application that I want to run some system tests on,
I have a web application that I want to sync with Flickr. I don't
In my web application, I create some framework that use to bind model data
I'm developing a web application in which the model consists a Group which contains
In my web application one model uses identifier that was generated by some UUID
I am developing a Backbone web application and I want to know that how
In my web application I have a model User. It's quite common that you

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.