zoukankan      html  css  js  c++  java
  • CString.format的用法

    在MFC程序中,使用CString来处理字符串是一个很不错的选择。CString既可以处理Unicode标准的字符串,也可以处理ANSI标准的字符串。CString的Format方法给我们进行字符串的转换带来了很大的方便,比如常见的int、float和double这些数字类型转换为CString字符串只需一行代码就可以实现。

      先看看Format用于转换的格式字符:

      %c                 单个字符

      %d                 十进制整数(int)

      %ld                十进制整数(long)

      %f                 十进制浮点数(float)

      %lf                十进制浮点数(double)

      %o                 八进制数

      %s                 字符串

      %u                 无符号十进制数

      %x                 十六进制数

      1、int转换为CString:

      CString str;

      int number=15;

      //str="15"

      str.Format(_T("%d"),number);

      //str=" 15"(前面有两个空格;4表示将占用4位,如果数字超过4位将输出所有数字,不会截断)

      str.Format(_T("%4d"),number);

      //str="0015"(.4表示将占用4位,如果数字超过4位将输出所有数字,不会截断)

      str.Format(_T("%.4d"),number);

      long转换为CString的方法与上面相似,只需要把%d改为%ld就可以了。

      2、double转换为CString:

      CString str;

      double num=1.46;

      //str="1.46"

      str.Format(_T("%lf"),num);

      //str="1.5"(.1表示小数点后留1位,小数点后超过1位则四舍五入)

      str.Format(_T("%.1lf"),num);

      //str="1.4600"

      str.Format(_T("%.4f"),num);

      //str=" 1.4600"(前面有1个空格)

      str.Format(_T("%7.4f"),num);

      float转换为CString的方法也同上面相似,将lf%改为f%就可以了。

      3、将十进制数转换为八进制:

      CString str;

      int num=255;

      //str="377"

      str.Format(_T("%o"),num);

      //str="00000377"

      str.Format(_T("%.8o"),num);

  • 相关阅读:
    nowcoderD Xieldy And His Password
    Codeforces681D Gifts by the List
    nowcoder80D applese的生日
    Codeforces961E Tufurama
    Codeforces957 Mahmoud and Ehab and yet another xor task
    nowcoder82E 无向图中的最短距离
    nowcoder82B 区间的连续段
    Codeforces903E Swapping Characters
    Codeforces614C Peter and Snow Blower
    Codeforces614D Skills
  • 原文地址:https://www.cnblogs.com/dawnpower/p/3631586.html
Copyright © 2011-2022 走看看