2

In .Net 4, Double.PositiveInfinity returns ∞

Is there a method to display "Infinity"

using System; public class Program { public static void Main() { Console.WriteLine("PositiveInfinity {0}.", (Double.PositiveInfinity)); } } 

expected is PositiveInfinity Infinity., but the actual output is PositiveInfinity ∞.

3
  • 2
    Just use a conditional statement like double x = double.PositiveInfinity; Console.WriteLine("PositiveInfinity {0}.", Double.PositiveInfinity == x ? "Infinity": x.ToString());... Commented Apr 19, 2019 at 5:17
  • 1
    @Chayim or double.IsPositiveInfinity(x) ? ... Commented Apr 19, 2019 at 5:18
  • 2
    Thanks, I wanted to know if there is a inbuilt method rather than hardcoding the value. because it used to display "infinity" for versions before .net 4 Commented Apr 19, 2019 at 5:19

1 Answer 1

8

This can be done by configuring the Current Thread culture. You can set your positive and negative infinity symbols there. Something like this:

var ci = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone(); ci.NumberFormat.NegativeInfinitySymbol = "-Infinity"; ci.NumberFormat.PositiveInfinitySymbol = "+Infinity"; Thread.CurrentThread.CurrentCulture = ci; Console.WriteLine("PositiveInfinity {0}.", (Double.PositiveInfinity)); 

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.