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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:05:47+00:00 2026-06-07T21:05:47+00:00

Currently, I’m using Jackson to send out JSON results from my Spring-based web application.

  • 0

Currently, I’m using Jackson to send out JSON results from my Spring-based web application.

The problem I’m having is trying to get all money fields to output with 2 decimal places. I wasn’t able to solve this problem using setScale(2), as numbers like 25.50 are truncated to 25.5 etc

Has anyone else dealt with this problem? I was thinking about making a Money class with a custom Jackson serializer… can you make a custom serializer for a field variable? You probably can… But even still, how could I get my customer serializer to add the number as a number with 2 decimal places?

  • 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-07T21:05:48+00:00Added an answer on June 7, 2026 at 9:05 pm

    You can use a custom serializer at your money field. Here’s an example with a MoneyBean. The field amount gets annotated with @JsonSerialize(using=…).

    public class MoneyBean {
        //...
    
        @JsonProperty("amountOfMoney")
        @JsonSerialize(using = MoneySerializer.class)
        private BigDecimal amount;
    
        //getters/setters...
    }
    
    public class MoneySerializer extends JsonSerializer<BigDecimal> {
        @Override
        public void serialize(BigDecimal value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
                JsonProcessingException {
            // put your desired money style here
            jgen.writeString(value.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        }
    }
    

    That’s it. A BigDecimal is now printed in the right way. I used a simple testcase to show it:

    @Test
    public void jsonSerializationTest() throws Exception {
         MoneyBean m = new MoneyBean();
         m.setAmount(new BigDecimal("20.3"));
    
         ObjectMapper mapper = new ObjectMapper();
         assertEquals("{\"amountOfMoney\":\"20.30\"}", mapper.writeValueAsString(m));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I'm using the Jackson JSON Processor to write preference data and whatnot to
Currently i'm having trouble using two functions in my -(void)viewDidLoad , both of these
Currently, I have a GWT based application /app.htm It displays an openId login button
Currently i am consuming web service from android by the method SOAP.Here i need
Currently writing a C# application it should do backups using GIT in the background.
I am currently running into a problem where an element is coming back from
Currently I am doing mass deletes using variations of the following query: DELETE FROM
Currently i am working on a Location based application for iPhone/iPad . I have
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently I am using HTML files for parts of my user interface. I display

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.