当前位置:网站首页>字符串格式化
字符串格式化
2022-07-07 08:06:00 【还是要努力呀!】
在程序编写过程中,需要将数值型变量按照一定的格式转化为字符串变量,下面介绍一些基本格式转换的语句:
格式化货币
string.Format("{0:C}",0.2); //¥0.20(英文操作系统下:$0.20)
该方法默认格式化小数点后面保留两位小数,如需保留一位或是更多,可以指定位数,截取时采用五舍六入法。
string.Format("{0:C1}",0.25); //¥0.2
string.Format("{0:C1}",0.26); //¥0.3
数字格式化
- 格式化固定位数的十进制数字
string.Format("{0:D3}",23); //023
string.Format("{0:D3}",2343); //2343
该方法可以将数字格式化为固定的位数,并且只支持整形。所指定的固定位数表明结果字符串中所需最少数字个数,当所指定的固定位数小于该数字本身的位数时,结果字符串为该数字本身。
- 使用逗号将数字分割并指定小数点后的位数
string.Format("{0:N}",230869.55); //230869.55
string.Format("{0:N1}",230869.55); //230869.5
该方法默认格式化小数点后面保留两位小数,如需保留一位或是更多,可以指定位数,截取时采用五舍六入法。
- 格式化为百分比
string.Format("{0:P}",0.23256); //23.26%
string.Format("{0:P1}",0.23255); //23.3%
该方法默认格式化小数点后面保留两位小数,如需保留一位或是更多,可以指定位数,截取时采用四舍五入法。
占位符
- 零占位符
string.Format("{0:0000.00}",23243.214); //023
string.Format("{0:0000.00}",243.215); //2343
该方法可以通过“在数位上置0”来设置数字的格式,目标数字在相应的数位上有数字时则保留该数字,无数字时则使用0填充。相当于“固定位数的十进制数字”的加强版。四舍五入。
2. 数字占位符
string.Format("{0:####.##}",23243.214); //023
string.Format("{0:####.##}",243.215); //2343
该方法与“零占位符”的效果类似,使用“#”来标识数字的形式,目标数字在相应的数位上有数字时则保留该数字,无数字时则省略该数位。
3. 空格占位符
string A = string.Format("{0,-25}", "左对齐,不足补空格");
A += "(结束)";
日期格式化
string.Format("{0:d}", DateTime.Now); // 2022/6/11
string.Format("{0:g}", DateTime.Now); // 2022/6/11 15:58
string.Format("{0:G}", DateTime.Now); // 2022/6/11 15:58:16
string.Format("{0:D}", DateTime.Now); // 2022年6月11日,星期六
string.Format("{0:f}", DateTime.Now); // 2022年6月11日,星期六 15:58
string.Format("{0:F}", DateTime.Now); // 2022年6月11日,星期六 15:58:16
string.Format("{0:m}", DateTime.Now); // 6月11日
string.Format("{0:t}", DateTime.Now); // 15:58
string.Format("{0:T}", DateTime.Now); // 15:58:16
以上字符串格式化语句可以将非字符串的变量按照一定格式转化为字符串,对于本身就是字符串的变量来说无法取得转换效果。
边栏推荐
猜你喜欢
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Inno setup packaging and signing Guide
Enterprise practice | construction of banking operation and maintenance index system under complex business relations
mysql插入数据创建触发器填充uuid字段值
Some thoughts on the testing work in the process of R & D
高数_第1章空间解析几何与向量代数_向量的数量积
request对象对请求体,请求头参数的解析
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
ISP、IAP、ICP、JTAG、SWD的编程特点
fiddler-AutoResponder
随机推荐
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Programming features of ISP, IAP, ICP, JTAG and SWD
Appx code signing Guide
In addition to the objective reasons for overtime, what else is worth thinking about?
ISP、IAP、ICP、JTAG、SWD的编程特点
MCU is the most popular science (ten thousand words summary, worth collecting)
Introduction to uboot
Deadlock caused by non clustered index in SQL Server
“十二星座女神降临”全新活动推出
ORM--分组查询,聚合查询,查询集QuerySet对象特性
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
Some thoughts on the testing work in the process of R & D
Word自动生成目录的方法
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
STM32中AHB总线_APB2总线_APB1总线这些是什么
request对象对请求体,请求头参数的解析
Introduction to energy Router: Architecture and functions for energy Internet
Vs code specifies the extension installation location
The method of word automatically generating directory