Calculate a Ratio in C#

前端 未结 5 1979
半阙折子戏
半阙折子戏 2020-12-03 05:36

I thought this would be simple, but searching Google didn\'t seem to help.

I\'m basically trying to write a function which will return a ratio as a string (eg 4:3) w

5条回答
  •  心在旅途
    2020-12-03 06:03

    Other commentators have given good solutions for integers; if you really have to deal with floating-point values, though, you'll need something else. In general, two real numbers won't have a clean ratio that can be prettily printed; what you want is the closest rational approximation. Probably the best way to go about finding that is just to compute the continued fraction expansion of the quotient; Mark Dominus gives a good introduction to those on his blog.

提交回复
热议问题