How to type π (pi) in Java?

后端 未结 3 1058
深忆病人
深忆病人 2020-12-20 14:53

Is there a character within the Math API that allows for representing the character π?

相关标签:
3条回答
  • 2020-12-20 14:59

    "\u03C0"

    http://www.fileformat.info/info/unicode/char/3c0/index.htm

    0 讨论(0)
  • 2020-12-20 15:09

    If you want to write this to a file you can achieve it through Unicodes, below is an example of how,

    import java.io.*;
    public class Test {
    
        public static void main(String[] args) throws Exception{
            BufferedWriter out = 
                        new BufferedWriter(new OutputStreamWriter(
                              new FileOutputStream("out.txt"),"UTF8"));     
    
        out.write("alpha = \u03B1, beta = \u03B2 ,pi = \u03C0");
        out.flush();
        out.close();
    }
    }
    

    out put : alpha = α, beta = β ,pi = π

    0 讨论(0)
  • 2020-12-20 15:12

    You don't even need to use the Math API. In Java source code, this:

    \u03C0
    

    is equivalent to this:

    π
    

    and you can even use it in an identifier; these two are equivalent:

    final String \u03C0 = "\u03C0";
    final String π = "π";
    

    (As the spec puts it:

    A Unicode escape of the form \uxxxx, where xxxx is a hexadecimal value, represents the UTF-16 code unit whose encoding is xxxx. This translation step allows any program to be expressed using only ASCII characters.

    See http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.2.)

    0 讨论(0)
提交回复
热议问题