int i=10;
方法1:Console.WriteLine(i.ToString("D5"));
方法2:Console.WriteLine(i.ToString().PadLeft(5,'0'));//推荐
方法3:Console.WriteLine(i.ToString("00000")); 

 

在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位。

PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddingChar 补足 totalWidth 长度
PadLeft(int totalWidth, char paddingChar) //在字符串右边用 paddingChar 补足 totalWidth 长度
示例:
h = h.PadLeft(2, '0');
注意第二个参数为 char 类型,所以用单引号,也可以用 Convert.ToChar(string value) 把字符串转换成 char 类型。如果字符串长度大于 1,则使用 str.ToCharArray()[index]。

C# String 前面不足位数补零的方法的更多相关文章

  1. C# String 前面不足位数补零的方法 PadLeft

    PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddingChar 补足 totalWidth 长度PadLeft(int totalWid ...

  2. 【转】C# String 前面不足位数补零的方法

    源地址:http://www.cnblogs.com/zhangqs008/archive/2012/02/01/2341078.html

  3. oracle不足位数补零的实现sql语句

    select rpad('AAA',5,'0') from dual; 这样就可以了 [注意] 1.'AAA'为待补字符:5表示补齐后的总字符长度:0表示不足时补什么字符 2.rpad是右侧补0,左侧 ...

  4. java中int转String 固定位数 不足补零

    转载自:http://ych0108.iteye.com/blog/2174134 String.format("%010d", 25); //25为int型 0代表前面要补的字符 ...

  5. java中int转成String位数不足前面补零

    java中int转成String位数不足前面补零 转载自:http://ych0108.iteye.com/blog/2174134 java中int转String位数不够前面补零 String.fo ...

  6. 【c++基础】int转string自动补零

    前言 使用to_string函数可以将不同类型的数据转换为string类,请参考here和here.如果string的位数固定,如何进行自动补零呢?请看本文实例! 代码 确定位数,to_string ...

  7. C#位数不足补零

    C#位数不足补零:int i=10;方法1:Console.WriteLine(i.ToString("D5"));方法2:Console.WriteLine(i.ToString ...

  8. 047-PHP数字前面补零,固定位数补0

    <?php #PHP 数字前面补零 固定位数补0 $num=128; $num=str_pad($num,8,"0",STR_PAD_LEFT); echo $num; // ...

  9. 时间戳位数不够13位,通过es6 的padEed补零

    if(val.toString().length == 10){ val = val.toString().padEnd(13,"0")*1 //不够十三位放后面补零,超过13位也 ...

随机推荐

  1. MSSQL-to-MySQL v5.3, 从MSSQL迁移到mySQL的最佳工具

    将现有的MSSQL数据库迁移到MySQL数据库,尝试了很多种工具 MySQL Workbench / MSSQL to MySQL Export / DB Converter / openDBcopy ...

  2. spring quartz分布式任务计划

    spring quartz分布式任务计划 环境: 通过maven管理的spring mvc工程,且已经成功连接数据库. 数据库表结构 /*Table structure for table `qrtz ...

  3. ACM/ICPC 之 最小割转网络流(POJ3469)

    重点:构图 //最小割转网络流 //邻接表+Dinic //Time:5797Ms Memory:6192K #include<iostream> #include<cstring& ...

  4. Ehlib安装方法有窍门

    Ehlib安装方法有窍门,如果不知道该安装方法,很难成功安装,Delphi 7(D5.D6也如此)中的安装方法       1.把EhLib中的common和DataService文件拷贝到Delph ...

  5. struts2+jsp+hiberbate 双重遍历

    今天弄了个双重遍历,由于自己水平有限,做了好久才搞定. 例子如下:也不算是例子,简要代码吧 action中有属性: private List<Contents> content; 其中co ...

  6. spring 的IoC的个人理解

    1.ioc IoC的概念介绍 ( a).依赖注入, 分为依赖 和 注入  , 其实依赖是一种耦合方式, 通过spirng在运行时将这种依赖关系完成, 达到解耦目的, 同时达到代码重用, 方便测试,更加 ...

  7. win10取消vpn使用远程默认网关

    打开%AppData%\Microsoft\Network\Connections\Pbk,找到rasphone.pbk文件(当你新建拨号连接或者VPN连接之后会在这个目录创建一个.pbk的文件,这个 ...

  8. SPRING MVC总结

    DispatcherServlet -- 前置控制器HandlerMapping接口 -- 处理请求的映射HandlerMapping接口的实现类:DefaultAnnotationHandlerMa ...

  9. Google之Chromium浏览器源码学习——base公共通用库(四)

    本文将介绍debug调试相关的内容,包括调试器.性能分析.堆跟踪.跟踪事件等: alias.h:Alias函数,提供防止载微软的编译器优化某参数变量的操作,内部通过#pragma optimize(& ...

  10. Android真机调试 Android.Util.AndroidRuntimeException: You cannot combine custom titles with other title features

    参考连接:http://blog.csdn.net/scyatcs/article/details/9003285 Android.Util.AndroidRuntimeException: You ...