Why does integer division in C# return an integer and not a float?

前端 未结 8 889
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 04:32

Does anyone know why integer division in C# returns an integer and not a float? What is the idea behind it? (Is it only a legacy of C/C++?)

In C#:

fl         


        
8条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 05:13

    Might be useful:

    double a = 5.0/2.0;   
    Console.WriteLine (a);      // 2.5
    
    double b = 5/2;   
    Console.WriteLine (b);      // 2
    
    int c = 5/2;   
    Console.WriteLine (c);      // 2
    
    double d = 5f/2f;   
    Console.WriteLine (d);      // 2.5
    

提交回复
热议问题