Home > Java > javaTutorial > body text

How to convert char type to string in Java

王林
Release: 2023-04-22 12:43:07
forward
1787 people have browsed it

Code:

// 标准数字格式字符串用法说明及举例
class NumberFormat
{
  static void Main()
  {
    // {index[,alignment][:format]}
    // index     - 从零开始的整数,指示对象列表中要格式化的元素。
    // alignment - 可选整数,指示最小宽度,不足部分用空格填充。负数指示左对齐,正数指示右对齐。
    // format    - 格式字符串。标准数字格式字符串采取“Axx”形式,A为单个字母, 说明如下:
    //    c - 货币,     xx指示小数位数
    //    d - 十进制,   xx指示有效位数, 不足用零填充, 只能用于整数
    //    x - 十六进制, 用法同d
    //    e - 指数,     xx指示小数位数
    //    f - 固定点,   xx指示小数位数
    //    n - 数字,     输出千位分隔符, 其余同f
    //    g - 常规,     xx指示有效位数, 使用e或f中较短者
    //    p - 百分比,   xx指示小数位数
    // 例如以下语句的输出为: "|¥-5.00|-05| -05|-5.0 |-500%|12345.7|12,345.7|"
    System.Console.WriteLine("|{0:c}|{0:d2}|{0,4:d2}|{0,-5:f1}|{0:p0}|{1:f1}|{1:n1}|", -5, 12345.67);
  }
}
Copy after login

12345.ToString("n"); //Generate 12,345.00
12345.ToString("C"); //Generate ¥12,345.00
12345. ToString("e"); //Generate 1.234500e 004
12345.ToString("f4"); //Generate 12345.0000
12345.ToString("x"); //Generate 3039 (hexadecimal)
12345.ToString("p"); //Generate 1,234,500.00%

Convert percentage to numeric type

Convert.ToDouble(s.TrimEnd('%')) / 100;

The above is the detailed content of How to convert char type to string in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template