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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:02:33+00:00 2026-05-13T14:02:33+00:00

I receive a decimal number with a maximum of 4 digits after the .

  • 0

I receive a decimal number with a maximum of 4 digits after the “.” and I know this number is in milligram.

I have to find the best matching unit (milligram, gram, kilogram) for the number.

for an example if I receive

edited

116000000.0000 milligram, it’s going to return 116.0000 kilogram
66990000.0000 milligram, it’s going to return 66.9900 kilogram
49000010.0000 milligram, it’s going to return 49000.0100 g
49000000.0100 milligram, it’s going to return 49000000.0100 milligram
1001 milligram, it’s going to return 1.0010 gram
1010 milligram, it’s going to return 1.0100 gram
1000 milligram, it’s going to return 0.0010 kilogram
1100 milligram, it’s going to return 0.0011 kilogram
135005 milligram, it’s going to return 135.0050 gram
and last sample 10013500 milligram, it’s going to return 10.0135 kilogram

I’m currently using this code, which I think look/is ugly and can fail

Dim temp As Decimal
Dim u = New List(Of Integer)(New Integer() {1, 1000, 1000000})
For i = 0 To u.Count - 1
    temp = CDec(qty / u(i))
    If (temp * 10000) - Math.Truncate(temp * 10000) <> 0 AndAlso (temp * 10000) - Math.Truncate(temp * 10000) < 1 Then
        temp = CDec(qty / u(i - 1))
        Exit For
    End If
Next
qty = temp

is there a better/nicer way of doing what I do?

edit for precision

the input can be any decimal between 0.0001 and maximum that a decimal can accept in .net

the output need to be rounded to the best unit with a maximum of 4 digits after “.” without losing any precision

  • 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-13T14:02:34+00:00Added an answer on May 13, 2026 at 2:02 pm

    Gen the numbers and choose the suitable one.

        public static decimal FormatDecimal(decimal i)
        {
            decimal milli = i;
            decimal grams = decimal.Round(i / 1000m, 4);
            decimal kilo = decimal.Round(grams / 1000m, 4);
    
            if (kilo * 1000 * 1000 == milli)
            {
                return kilo;
            }
            if (grams * 1000 == milli)
            {
                return grams;
            }
            return milli;
        }
    

    And to test:

        public static void FormatDecimalTest()
        {
            if (FormatDecimal(116000000.0000m) == 116.0000m)
                Console.WriteLine("ok1");
            if (FormatDecimal(66990000.0000m) == 66.9900m)
                Console.WriteLine("ok2");
            if (FormatDecimal(49000010.0000m) == 49000.0100m)
                Console.WriteLine("ok3");
            if (FormatDecimal(49000000.0100m) == 49000000.0100m)
                Console.WriteLine("ok4");
            if (FormatDecimal(1001m) == 1.0010m)
                Console.WriteLine("ok5");
            if (FormatDecimal(1000m) == 0.0010m)
                Console.WriteLine("ok6");
            if (FormatDecimal(1100m) == 0.0011m)
                Console.WriteLine("ok7");
            if (FormatDecimal(1100m) == 0.0011m)
                Console.WriteLine("ok8");
            if (FormatDecimal(135005m) == 135.0050m)
                Console.WriteLine("ok9");
            if (FormatDecimal(10013500m) == 10.0135m)
                Console.WriteLine("ok10");
        }
    

    In your question, I see you used a loop over the various factors. Here’s a looping solution that will find the first factor that does not lose precision.

        public static decimal FormatDecimal(decimal i)
        {
            List<decimal> myFactors = new List<decimal>()
            { 1000m * 1000m, 1000m};
    
            foreach (decimal conversionFactor in myFactors)
            {
                decimal result = decimal.Round(i / conversionFactor, 4);
                if (result * conversionFactor == i)
                {
                    return result;
                }
            }
    
            return i;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's see if I can explain myself, I have this models: class BillHeader(models.Model): number
I have a problem for the managemente of decimal number in java (JDK 1.4).
I receive this error when I click on the section of my Tab-Bar application
Im parsing xml using php and one of my variables receive a long decimal
I have defined a column in SQL to be decimal(4,1), null which means I
Using XSLT 1.0 & receive dates in the following format: 20111129060804 and I have
I receive hexadecimal number string from Matlab into my device . I check the
First of all, i have an output array like this (in the matrix of
Hello I have a string parser who receives as its input a hex number:
I am trying to subtract two numbers and get a result of decimal number

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.