当前位置: 首页 > news >正文

C#中ToString格式大全

更多资源:http://denghejun.github.io

C

 

 

货币

 

 

2.5.ToString("C")

 

 

¥2.50

 

 

D

 

 

十进制数

 

 

25.ToString("D5")

 

 

00025

 

 

E

 

 

科学型

 

 

25000.ToString("E")

 

 

2.500000E+005

 

 

F

 

 

固定点

 

 

25.ToString("F2")

 

 

25.00

 

 

G

 

 

常规

 

 

2.5.ToString("G")

 

 

2.5

 

 

N

 

 

数字

 

 

2500000.ToString("N")

 

 

2,500,000.00

 

 

X

 

 

十六进制

 

 

255.ToString("X")

 

 

FF

 

formatCode 是可选的格式化代码字符串。(详细内容请搜索“格式化字符串”查看)

必须用“{”和“}”将格式与其他字符分开。如果恰好在格式中也要使用大括号,可以用连续的两个大括号表示一个大括号,即: “{{”或者“}}”。

常用格式举例:

(1) int i=12345;

this.textBox1.Text=i.ToString();

//结果 12345(this指当前对象,或叫当前类的实例)

this.textBox2.Text=i.ToString("d8");

//结果 00012345

(2) int i=123;

double j=123.45;

string s1=string.Format("the value is {0,7:d}",i);

string s2=string.Format("the value is {0,7:f3}",j);

this.textBox1.Text=s1 ;

//结果 the value is 123

this.textBox2.Text=s2;

//结果 the value is 123.450

(3)double i=12345.6789;

this.textBox1.Text=i.ToString("f2"); //结果 12345.68

this.textBox2.Text=i.ToString("f6");

//结果 12345.678900

(4)double i=12345.6789;

this.textBox1.Text=i.ToString("n"); //结果 12,345.68

this.textBox2.Text=i.ToString(“n4”); //结果 12,345.6789

(5)double i=0.126;

string s=string.Format("the value is {0:p}",i);

this.textBox1.Text=i.ToString("p"); //结果 12.6%

this.textBox2.Text=s; //结果 the value is 12.6%

(6) DateTime dt =new DateTime(2003,5,25);

this.textBox1.Text=dt.ToString("yy.M.d");

//结果 03.5.25

this.textBox2.Text=dt.ToString(“yyyy年M月”);

//结果 2003年5月

Convert.ToDateTime("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
"2005/12/22 22:22:22"

(7) int i=123;

double j=123.45;

string s=string.Format("i:{0,-7},j:{1,7}",i,j);

//-7表示左对齐,占7位

this.textBox1.Text=s ;

//结果i:123 ,j: 123.45 

 

Byte.ToString 方法 (String, IFormatProvider)的C#例子

 

下面的代码示例使用 ToString 方法的几个重载格式化 Byte 值。

// Example for the Byte.ToString( ) methods.
using System;
using System.Globalization;

class ByteToStringDemo
{
    static void RunToStringDemo( )
    {
        byte smallValue = 13;
        byte largeValue = 234;
            
        // Format the Byte values without and with format strings.
        Console.WriteLine( "/nIFormatProvider is not used:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( ), 
            largeValue.ToString( ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'X2' format string:", smallValue.ToString( "X2" ), 
            largeValue.ToString( "X2" ) );
            
        // Get the NumberFormatInfo object from the 
        // invariant culture.
        CultureInfo         culture = new CultureInfo( "" );
        NumberFormatInfo    numInfo = culture.NumberFormat;
            
        // Set the digit grouping to 1, set the digit separator
        // to underscore, and set decimal digits to 0.
        numInfo.NumberGroupSizes = new int[ ] { 1 };
        numInfo.NumberGroupSeparator = "_";
        numInfo.NumberDecimalDigits = 0;
            
        // Use the NumberFormatInfo object for an IFormatProvider.
        Console.WriteLine( 
            "/nA NumberFormatInfo object with digit group " +
            "size = 1 and /ndigit separator " +
            "= '_' is used for the IFormatProvider:" );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "No format string:", smallValue.ToString( numInfo ), 
            largeValue.ToString( numInfo ) );
        Console.WriteLine( "   {0,-20}{1,10}{2,10}", 
            "'N' format string:", 
            smallValue.ToString( "N", numInfo ), 
            largeValue.ToString( "N", numInfo ) );
    } 
        
    static void Main( )
    {
        Console.WriteLine( "This example of/n" +
            "   Byte.ToString( ),/n" +
            "   Byte.ToString( String ),/n" + 
            "   Byte.ToString( IFormatProvider ), and/n" +
            "   Byte.ToString( String, IFormatProvider )/n" +
            "generates the following output when formatting " +
            "Byte values /nwith combinations of format " +
            "strings and IFormatProvider." );
            
        RunToStringDemo( );
    }
}

/*
This example of
   Byte.ToString( ),
   Byte.ToString( String ),
   Byte.ToString( IFormatProvider ), and
   Byte.ToString( String, IFormatProvider )
generates the following output when formatting Byte values
with combinations of format strings and IFormatProvider.

IFormatProvider is not used:
   No format string:           13       234
   'X2' format string:         0D        EA

A NumberFormatInfo object with digit group size = 1 and
digit separator = '_' is used for the IFormatProvider:
   No format string:           13       234
   'N' format string:         1_3     2_3_4
*/

 

转载于:https://www.cnblogs.com/denghejun/p/3849181.html

相关文章:

  • mvc
  • 51单片机-IO口
  • json.net 比jsonIgnore 更好的方法 修改源码
  • leetcode 栈 括号匹配
  • 周立功写给学单片机的年轻人
  • nginx配置文件中的location详解
  • CakePHP 编程笔记3
  • Floyd模板(详细操作最基础版)
  • Sendmail大全
  • 内核配置备份
  • Query 使用手册
  • 循环冗余校验码CRC,求解步骤
  • 求职中的平常心——Leo网上答疑48
  • 实验三 数据查询(4学时)
  • 北京簋街 美食完全攻略 + 簋街好吃的夜宵去处-----店铺介绍大全
  • 3.7、@ResponseBody 和 @RestController
  • Android路由框架AnnoRouter:使用Java接口来定义路由跳转
  • javascript面向对象之创建对象
  • JavaScript设计模式与开发实践系列之策略模式
  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • js中forEach回调同异步问题
  • mongo索引构建
  • mysql 数据库四种事务隔离级别
  • Mysql5.6主从复制
  • React-flux杂记
  • Redis 懒删除(lazy free)简史
  • Tornado学习笔记(1)
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • webpack+react项目初体验——记录我的webpack环境配置
  • 阿里云前端周刊 - 第 26 期
  • 给初学者:JavaScript 中数组操作注意点
  • 关于Flux,Vuex,Redux的思考
  • 基于Android乐音识别(2)
  • 离散点最小(凸)包围边界查找
  • 前端性能优化——回流与重绘
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 赢得Docker挑战最佳实践
  • 【运维趟坑回忆录】vpc迁移 - 吃螃蟹之路
  • linux 淘宝开源监控工具tsar
  • 阿里云服务器如何修改远程端口?
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • ​如何在iOS手机上查看应用日志
  • ​一帧图像的Android之旅 :应用的首个绘制请求
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • $.proxy和$.extend
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (二)丶RabbitMQ的六大核心
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (十三)Flask之特殊装饰器详解
  • (一)kafka实战——kafka源码编译启动