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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:37:19+00:00 2026-06-10T20:37:19+00:00

I have a class public class Money { public string Currency { get; set;

  • 0

I have a class

public class Money
{
    public string Currency { get; set; }
    public decimal Amount { get; set; }
}

and would like to serialize it to JSON. If I use the JavaScriptSerializer I get

{"Currency":"USD","Amount":100.31000}

Because of the API I have to conform to needs JSON amounts with maximum two decimal places, I feel it should be possible to somehow alter the way the JavaScriptSerializer serializes a decimal field, but I can’t find out how. There is the SimpleTypeResolver you can pass in the constructor, but it only work on types as far as I can understand. The JavaScriptConverter, which you can add through RegisterConverters(…) seems to be made for Dictionary.

I would like to get

{"Currency":"USD","Amount":100.31}

after I serialize. Also, changing to double is out of the question. And I probably need to do some rounding (100.311 should become 100.31).

Does anyone know how to do this? Is there perhaps an alternative to the JavaScriptSerializer that lets you control the serializing in more detail?

  • 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-10T20:37:21+00:00Added an answer on June 10, 2026 at 8:37 pm

    In the first case the 000 does no harm, the value still is the same and will be deserialized to the exact same value.

    In the second case the JavascriptSerializer will not help you. The JavacriptSerializer is not supposed to change the data, since it serializes it to a well-known format it does not provide data conversion at member level (but it provides custom Object converters). What you want is a conversion + serialization, this is a two-phases task.

    Two suggestions:

    1) Use DataContractJsonSerializer: add another property that rounds the value:

    public class Money
    {
        public string Currency { get; set; }
    
        [IgnoreDataMember]
        public decimal Amount { get; set; }
    
        [DataMember(Name = "Amount")]
        public decimal RoundedAmount { get{ return Math.Round(Amount, 2); } }
    }
    

    2) Clone the object rounding the values:

    public class Money 
    {
        public string Currency { get; set; }
    
        public decimal Amount { get; set; }
    
        public Money CloneRounding() {
           var obj = (Money)this.MemberwiseClone();
           obj.Amount = Math.Round(obj.Amount, 2);
           return obj;
        }
    }
    
    var roundMoney = money.CloneRounding();
    

    I guess json.net cannot do this either, but I’m not 100% sure.

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

Sidebar

Related Questions

i have this class public class Image { public string url { get; set;
I have class: public class LoginResponse { public String SessionID { get; private set;
I have class public class Chat { public string Ip { get; set; }
I have a class: public class LevelInfo { public int Id { get; set;
i have a Class like public class user { public int ID { get;
I have class: public class MyClass { [XmlElement(Date)] public DateTime Date { get; set;
I have class Money which is an @Embeddable @Embeddable public class Money implements Serializable,
I have class with back reference: public class Employee : Entity { private string
i have a class: public class Vars { public static final String NAME =
I have class that looks like this: class A { public: class variables_map vm

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.