0

I am using the following code to convert cm to the meter.

 public static double? ConvertCmToM(double? cm)
        {
            return cm.Value * 0.01;
            
        }

When I input the number 8.8 output giving as

0.08800000000000001m

But I want to stop in the index where zero adds no value in the decimal part. In this case I want to display the value as

0.088m

This is already done in major converter websites. when you type the cm to m converter on google those sites will appear. How do they do it?

I took the same sample and put in their sites and this is how they show.

> 0.088m

I cannot blindly substring the value after converting to a string as the zero part will appear in 5th or 6th element. That also handled in those sites.

This is a double data type. the letter "m" concat at the last minute. How to acehive this?

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41
  • You have to convert to a string. Numbers are stored a base 2 while the display results is in base 10. Converting from base 2 to base 10 is not the solution. – jdweng Jan 08 '23 at 14:27
  • @jdweng with all due respect can you give me a sample, please? I cannot blindly substring to 3 or 4 decimal parts. – Prageeth Liyanage Jan 08 '23 at 14:30
  • Look at this question: https://stackoverflow.com/questions/4525854/remove-trailing-zeros – Icemanind Jan 08 '23 at 14:31
  • Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Heretic Monkey Jan 08 '23 at 14:35
  • You are solving the wrong problem. The real issue is the rounding of of binary numbers to decimal. First determine the number of decimal places you need. Than round number to the number of decimal places you actually need. It is not a good idea to truncate least significant zeroes where is some case a number will have 3 decimal (123.001) places and other case have 7 decimal places (123,0000001). In your case the rounding issue is occurring in the 16th decimal place. – jdweng Jan 08 '23 at 14:52

1 Answers1

3

I would use decimal instead of double then:

public static decimal? ConvertCmToM(decimal? cm)
{
    if(cm == null) return null;
    return Decimal.Multiply(cm.Value, 0.01m);
}

static void Main()
{
    Console.Write(ConvertCmToM(8.8m)); // 0.088
}

decimal vs double! - Which one should I use and when?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • This is worked 100%. Thanks. I accepted your answer. Can you kindly explain how this solved the problem? how the decimal type solved the problem? what is this Decimal.Multiply? – Prageeth Liyanage Jan 08 '23 at 15:00
  • @PrageethLiyanage: have you read the link i have posted? Well, double is a binary floating point type and decimal is a floating decimal point type. So double represents numbers in binary format like 1001.101011001 whereas double represents numbers like `457.85`(for example money). [`Decimal.Multiply`](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.multiply?view=net-7.0) is nothing special, a method to multiply two decimals that returns decimal. Otherwise you have to cast your values to decimal to prevent that double is used. – Tim Schmelter Jan 08 '23 at 17:55
  • Why are you using `Decimal.Multiply` instead of `*`? Looks a bit confusing given both operands are decimals. – Evk Jan 08 '23 at 18:03
  • @evk: well in this case there is no difference, but maybe you don't have decimal literals but other types and you want to enforce decimal multiplication. By the way, if you multiply two decimals `Decimal.Multiply` is used as well, because the `*`-operator is overloaded. If you would not use `Multiply` you could get this weird behavior: https://dotnetfiddle.net/KzZLVB – Tim Schmelter Jan 08 '23 at 18:38