首页 > 编程语言 >C++数据格式化1 - uint转换成字符串 & double转换成字符串

C++数据格式化1 - uint转换成字符串 & double转换成字符串

时间:2024-06-18 22:54:00浏览次数:12  
标签:std 转换成 string double fmt formatted 字符串 include

1. 关键词

C++ 数据格式化 字符串处理 std::string int double 跨平台

2. strfmt.h

#pragma once

#include <string>
#include <cstdint>
#include <sstream>
#include <iomanip>

namespace cutl
{
    /**
     * @brief Format uint64 value to a string with a given width and fill character.
     *
     * @param val the value to be formatted.
     * @param width the width of the formatted string.
     * @param fill the fill character of the formatted string, default is '0'.
     * @return std::string the formatted string.
     */
    std::string fmt_uint(uint64_t val, uint8_t width = 0, char fill = '0');
    /**
     * @brief Format double value to a string with a given precision.
     *
     * @param val the value to be formatted.
     * @param precision the precision of the formatted string, default is 2.
     * @return std::string the formatted string.
     */
    std::string fmt_double(double val, int precision = 2);
} // namespace cutl

3. strfmt.cpp

#include <sstream>
#include <iomanip>
#include <bitset>
#include "strfmt.h"

namespace cutl
{
    std::string fmt_uint(uint64_t val, uint8_t width, char fill)
    {
        std::stringstream ss;
        ss << std::setfill(fill) << std::setw(width) << val;
        return ss.str();
    }

    std::string fmt_double(double val, int precision)
    {
        std::stringstream ss;
        ss << std::setiosflags(std::ios::fixed) << std::setprecision(precision) << val;
        return ss.str();
    }
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strfmt.h"

void TestFormatUintAndDouble()
{
    PrintSubTitle("TestFormatUintAndDouble");

    std::cout << "fmt_uint 1: " << cutl::fmt_uint(12) << std::endl;
    std::cout << "fmt_uint 2: " << cutl::fmt_uint(12, 4) << std::endl;
    std::cout << "fmt_uint 3: " << cutl::fmt_uint(12, 4, 'x') << std::endl;
    std::cout << "fmt_double 1: " << cutl::fmt_double(3.141592653) << std::endl;
    std::cout << "fmt_double 2: " << cutl::fmt_double(3.141592653, 3) << std::endl;
}

5. 运行结果

--------------------------------------TestFormatUintAndDouble---------------------------------------
fmt_uint 1: 12
fmt_uint 2: 0012
fmt_uint 3: xx12
fmt_double 1: 3.14
fmt_double 2: 3.142

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

标签:std,转换成,string,double,fmt,formatted,字符串,include
From: https://www.cnblogs.com/luoweifu/p/18255351

相关文章

  • Python编程基础:f-字符串格式
    本文探讨使用Pythonf-字符串格式,也称为“格式化字符串文字”。f-string是格式化字符串的一种很好且简单的方法,适用于Pythonv3.6+。如果你仍然使用.format()方法,必须了解f-字符串。使用字符串格式的优势之一是能够“插入”并格式化字符串数据中的变量。Python字符串format()方......
  • 关于几种语言(c#,php,python,javascript)字符串的gzip压缩与解压的整理
    背景介绍因为一直在处理restfulAPI,给移动端提供的数据需要考虑流量问题,优先考虑就是压缩现有的字符串,然后再考虑业务逻辑方面的减少流量。鉴于找这些资料也花了不少时间,所以整理了这篇文章,留作纪念。参考网址PHP与C#的压缩与解压http://www.oschina.net/question/2265205_181......
  • 如何将webp格式转换成jpg格式?
    图片格式除了最常见的jpg和png外,还有很多不太经常使用到的格式,webp就是这样一种,它在某些旧版的操作系统、浏览器和图像处理软件中可能不被完全支持。通过将WebP格式转换为JPG格式,可以增加图片在不同平台和设备上的兼容性,确保图片能够正常显示和使用。那么,如何将WebP转JPG呢?接着往......
  • SAP ABAP 字符串内数字去前导零的两种方法(正则/拼接)
    碰到一个小需求,需要把字符串里数字的前导0给去掉。ABAP内,想对字符串做处理,把字符串内的数字进行去前导0,通常有两种思路,拆分处理再拼接或者直接用正则表达替换。最初的想法是直接通过符号拆分,去前导0后拼回去,但是了解到实际分割的符号并不唯一。所以直接用正则表达式来做替换会......
  • 使用 shell 快速生成字符串的哈希值
    使用方式echo-n"dev"|sha256sum|cut-d''-f1此外也可以使用md5sum、sha224sum、sha1sum等,替换命令中的sha256sum即可。命令解释echo将字符串"dev"通过管道符传递给标准输出,-n选项可以去掉多余的换行符sha256sum本身接收的参数是文件路径,如果不指定,则从标......
  • 简单处理字符串——6.14山大软院项目实训1
    对于直接输出服务器返回的json到Debug,发现他还包含json的结构,但是不想调试json的返回结构,可以使用简单地处理字符串的方法,而不引入额外的库或复杂的JSON解析,但是这个解决方式是暂时的是投机取巧的,正确的做法我会在下一条博客里面写出来。可以考虑使用字符串操作方法直接从接收......
  • (算法)找到字符串中所有字母异位词——<滑动窗⼝+哈希表>
    1.题⽬链接:438.找到字符串中所有字⺟异位词2.题⽬描述:3.解法(滑动窗⼝+哈希表): 算法思路:◦因为字符串p的异位词的⻓度⼀定与字符串p的⻓度相同,所以我们可以在字符串s中构造⼀个⻓度为与字符串p的⻓度相同的滑动窗⼝,并在滑动中维护窗⼝中每种字⺟的数量; ◦当窗......
  • 进行一个字符串算法的总结
    本文参考字符串基础byAlex_Wei。Manacher算法这玩意是用来求回文子串的。虽然一个字符串的子串数量是\(O(n^2)\)级别的,但是回文串有更好的描述方式。注意到若一个子串\([l,r]\)是以\(mid\)为回文中心的回文串,那么将左端点和右端点朝着\(mid\)方向挪动若干单位也......
  • python字符串的一些操作实例
    已知字符串a=“aAsomr3idd4HGHbigs7Dlsf9YeAF”,要求如下1.请将a字符串的大写改为小写,小写改为大写。2.将a字符串的数字取出,并输出成一个新的字符串。3.将a字符串中的内容反向输出4.打印a字符串中所有奇数位上的字符(下标是1,3,5,7…位上的字符)5.将a字符串的所有偶数位上......
  • c语言连接两个字符串
    在C语言中,连接两个字符串可以使用标准库函数strcat。这个函数将一个字符串添加到另一个字符串的末尾。使用strcat时,需要确保目标字符串有足够的空间来容纳源字符串,否则可能会导致缓冲区溢出。下面是strcat函数的基本用法示例:#include<stdio.h>#include<string.h>......