AnswerBun.com

C# Change number of decimals show, but it always skips a number

Stack Overflow Asked by theoverly on September 18, 2020

I’m a beginner when it comes to programing, started learning C# just a few weeks ago.

I’m currently working on an asignment where I’m supposed to take an input from the user and change the number of decimals shown according to that number.

I have been toying with two methods within what I’ve learnt so far, but the problem is that whenever i try to set the number of decimals to show to over three it will start skipping numbers, so when I try to set it to four decimals, the real forth decimal will be skipped and replaced by the fifth, and so on…

Here’s one of the examples;

        double d1 = 0.123456789;
        string dtest0 = d1.ToString("f0");
        string dtest1 = d1.ToString("f1");
        string dtest2 = d1.ToString("f2");
        string dtest3 = d1.ToString("f3");
        string dtest4 = d1.ToString("f4");
        string dtest5 = d1.ToString("f5");
        string dtest6 = d1.ToString("f6");
        string dtest7 = d1.ToString("f7");
        string dtest8 = d1.ToString("f8");
        string dtest9 = d1.ToString("f9");
        Console.WriteLine(dtest0);//output 0
        Console.WriteLine(dtest1);//output 0,1
        Console.WriteLine(dtest2);//output 0,12
        Console.WriteLine(dtest3);//output 0,123
        Console.WriteLine(dtest4);//output 0,1235
        Console.WriteLine(dtest5);//output 0,12346
        Console.WriteLine(dtest6);//output 0,123457
        Console.WriteLine(dtest7);//output 0,1234568
        Console.WriteLine(dtest8);//output 0,12345679
        Console.WriteLine(dtest9);//output 0,123456789

This is the code I first tried where I’m taking input from user, same problem;

        double nr1 = 0.123456789
        Console.Write("Add request phrase for input ");
        string input2 = (Console.ReadLine());
        int nr2 = Convert.ToInt32(input2);
        switch (nr2)
        {
            case 0:
                Console.WriteLine(nr1.ToString("##."));//Works
                break;

            case 1:
                Console.WriteLine(nr1.ToString("##.#"));//Works
                break;

            case 2:
                Console.WriteLine(nr1.ToString("##.##"));//Works
                break;

            case 3:
                Console.WriteLine(nr1.ToString("##.###"));//Works
                break;

            case 4:
                Console.WriteLine(nr1.ToString("##.####"));//Skips nr 4 but gives 4 decimals(0,1235)
                break;

            case 5:
                Console.WriteLine(nr1.ToString("##.#####"));//Skips nr 5 but gives 5 decimals(0,12346)
                break;

            case 6:
                Console.WriteLine(nr1.ToString("##.######"));//Skips nr 6 but gives 6 decimals(0,123457)
                break;

            case 7:
                Console.WriteLine(nr1.ToString("##.#######"));//Skips nr 7 but gives 7 decimals(0,1234568)
                break;

            case 8:
                Console.WriteLine(nr1.ToString("##.########"));//Skips nr 8 but gives 8 decimals(0,12345679)
                break;

            case 9:
                Console.WriteLine(nr1.ToString("##.#########"));//Works
                break;

            default:
                Console.WriteLine("Add answer for unexpected input");
                break;
        }

Can anyone help me figure out what I’m doing wrong and explain why the numbers are switching out?

Add your own answers!

Related Questions

How to center a flex container but left-align flex items

7  Asked on December 22, 2021 by etienne-nol

     

about IComponentConnector2

0  Asked on December 22, 2021 by yushang

   

WooCommerce order again and custom item data for price calculation

1  Asked on December 22, 2021 by elizabeth-parodi-roman

         

ruby how to stop the execution after rescue

1  Asked on December 22, 2021 by jill448

     

LSTM predicting constant value throughout

0  Asked on December 22, 2021 by harrys

         

Use main scrollbar to scroll all elements in the page

1  Asked on December 22, 2021 by marsilinou-zaky

         

How to read file from createReadStream in Node.js?

3  Asked on December 22, 2021 by ton1

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir