有没有 C++ fmt 库格式相当于用于写入格式化输出的 Fortran 比例因子 (P) 编辑描述符?
对于写入格式化输出,Fortran 有 P 编辑描述符,它将结果按 10 倍缩放。例如:
Program Test
Real a;
a = 123.456
write(6, "(3p, f12.2)") a;
End Program Test
返回:
123456.00
我正在研究使用 fmt 库编写等效 C++ 格式写入语句的方法。实际上,我正在编写一个脚本,用 C++ 等效项替换数千条 Fortran 语句。我正在考虑的当前方法(如下所示)将变量乘以适当的 10 次方将使事情变得复杂,因为我正在解析的代码是如何设置的。
#include <iostream>
#include <fstream>
#define FMT_HEADER_ONLY
#include "fmt/format.h"
#include "fmt/ostream.h"
int main()
{
float a = 123.456;
std::ofstream newfile ("newfile.txt");
fmt::print(newfile, "{:12.2f}", pow(10, 3) * a);
}
如果有一种简单的方法来缩放格式定义内的值(即 fmt::print 语句的第二个参数“{:12.2f}”内的值),那么对我的脚本来说将非常方便。但到目前为止,我所想到的只是将相关变量乘以适当的 10 次方,然后使用该值的方法。
For writing formatted output, Fortran has the P edit descriptor that will scale the result by factor(s) of 10. For example:
Program Test
Real a;
a = 123.456
write(6, "(3p, f12.2)") a;
End Program Test
Returns:
123456.00
I am looking into ways to write an equivalent C++ formatted write statement using the fmt library. In actuality, I am writing a script to replace several thousand of these Fortran statements with C++ equivalents. The current method I'm considering (shown below) of multiplying the variable by the appropriate power of 10 will make life complicated because of how the code I'm parsing is set up.
#include <iostream>
#include <fstream>
#define FMT_HEADER_ONLY
#include "fmt/format.h"
#include "fmt/ostream.h"
int main()
{
float a = 123.456;
std::ofstream newfile ("newfile.txt");
fmt::print(newfile, "{:12.2f}", pow(10, 3) * a);
}
It would be very convenient for my script if there was an easy way to scale the values inside the format definition, i.e., within the second argument, "{:12.2f}", of the fmt::print statement. So far though, all I've come up with is the shown method of multiplying the relevant variable by the appropriate power of 10 and then using that value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论