c语言 int 前面加 0,【已解决】C#中格式化输出int整型变量为固定宽度前加0的形式,即printf中%02d之类的形式...

本文介绍了如何在C#中格式化输出int整型变量为固定宽度并前加0,类似C语言printf的%02d形式。通过`String.Format("{0:00}", intVal)`实现将4格式化为04。" 112219911,10325247,本地开发环境搭建:nGrinder 教程,"['IDEA', 'nGrinder', '开发环境配置', 'maven配置', 'Java']
摘要由CSDN通过智能技术生成

【问题】

C#中,有个变量,为int型的4,想要格式化输出为04,相当于普通C语言中的printf中的%02d。

【解决过程】

1.已经找到之前的关于C#中的类似于printf中的用法了:string spritfTestStr = String.Format("Test sprintf in C#, number={0:D}, string=\"{1:s}\", float={2:0.000}", 100, "crifan", Math.PI);

//spritfTestStr = Test sprintf in C#, number=100, string="crifan", float=3.142

但是,此处尝试着写了多种方式:{0:2D}

{0:02D}

{0:02d}

竟然都还是无法正常输出我要的%02d的效果。Format specifier

Name

Description

0

Zero placeholder

If the value being formatted has a digit in the position where the ‘0’ appears in the format string, then that digit is copied to the result string. The position of the leftmost ‘0’ before the decimal point and the rightmost ‘0’ after the decimal point determines the range of digits that are always present in the r

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值