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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:46:35+00:00 2026-05-20T04:46:35+00:00

In Java you can use BigDecimal.stripTrailingZeros() to remove any extra zeros at the end.

  • 0

In Java you can use BigDecimal.stripTrailingZeros() to remove any extra zeros at the end.

I’ve read a few questions on here about how to strip trailing zeros from a decimal in C# but none of them seem to offer correct solutions.

This question for example the answer of doing ToString(“G”) doesn’t always work.

Ideally I would like a function that does decimal -> decimal with the new decimal having the least scale possible without losing any info or removing any trailing zeros.

Is there any way to do this easily? Or would it involve fiddling around with decimal.GetBits()?

EDIT: Should also add I have i18n to worry about so doing string manipulation on the result isn’t ideal because of differences in decimal separators.

  • 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-20T04:46:35+00:00Added an answer on May 20, 2026 at 4:46 am

    Here is what i have come up with (Crazy code but works smoothly)

    private decimal Normalize(decimal d)
    {
        string[] tmp = d.ToString().Split('.');
        string val = tmp[0];    
        string fraction = null;
        decimal result;
        if(tmp.Length > 1) fraction = tmp[1];
        if(fraction != null && Getleast(fraction) > 0) 
        {
            decimal.TryParse(val.ToString() + "." + fraction.TrimEnd('0').ToString(),out result);
        }
        else
        {
            return decimal.Parse(val);
        }
        return result;
    }
    
    private decimal Getleast(string str)
    {
        decimal res;
        decimal.TryParse(str.TrimEnd('0'),out res);// It returns 0 even if we pass null or empty string
        return res;
    }
    

    Here are sample Input:

    Console.WriteLine(Normalize(0.00M));
    Console.WriteLine(Normalize(0.10M));
    Console.WriteLine(Normalize(0001.00M));
    Console.WriteLine(Normalize(1000.01M));
    Console.WriteLine(Normalize(1.00001230M));
    Console.WriteLine(Normalize(0031.200M));        
    Console.WriteLine(Normalize(0.0004000M));
    Console.WriteLine(Normalize(123));
    

    And respective output:

    0
    0.1
    1
    1000.01
    1.0000123
    31.2
    0.0004
    123
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any free java library which I can use to convert string in
Is there a pure-Java equivalent to <jsp:forward page=... /> that I can use within
Can we use Java in Silverlight?
How can I use/display characters like ♥, ♦, ♣, or ♠ in Java/Eclipse? When
Simple as the title states: Can you use only Java commands to take a
In Java, we can always use an array to store object reference. Then we
In java a class can implement Iterable which lets you use the foreach() statement
Can anyone explain the use of ^ operator in java with some examples?
In Java, I use a class in which some fields can be null .
What is the use of anonymous classes in Java? Can we say that usage

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.