숫자가 그다지 크지 않을 때이 문제를 해결하는 많은 질문이 있다는 것을 알고 있지만, 큰 숫자 (여전히 float / double 범위에 있음)의 경우 출력이 정밀도를 잃을 것입니다.
#include <stdio.h>
#include <iostream>
#include <sstream>
int main() {
double d = -1e307, d2 = 1e307;
std::cout << std::fixed << d << std::endl;
printf("d2 is %lf\n", d2);
return 0;
}
이 코드는 다음과 같은 출력을 얻습니다.
-9999999999999999860310597602564577717002641838126363875249660735883565852672743849064846414228960666786379280392654615393353172850252103336275952370615397010730691664689375178569039851073146339641623266071126720011020169553304018596457812688561947201171488461172921822139066929851282122002676667750021070848.000000
d2 is 9999999999999999860310597602564577717002641838126363875249660735883565852672743849064846414228960666786379280392654615393353172850252103336275952370615397010730691664689375178569039851073146339641623266071126720011020169553304018596457812688561947201171488461172921822139066929851282122002676667750021070848.000000
그러나 이것은 분명히 정확하지 않습니다! 진짜 변환하는 방법?!
실제로 나는 이것을 내 자신을 처리했으며, 여기에 내 코드가 있으며, 함수는 임의의 부동 문자열을 일반 문자열로 변환합니다.
이 질문에서 영감을 얻은이 함수 .
// DW: remove e
std::string trim_real(std::string str) {
// DW: all to lowercase
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
// DW: find "e" and "."
size_t pe = str.find_first_of("e");
std::string se = str.substr(pe + 1, str.size() - (pe + 1));
int de = std::stoi(se);
// DW: get string without "e"
std::string sc = str.substr(0, pe);
std::cout << "sc is " << sc << std::endl;
// DW: remove uneccessary 0
size_t pf0 = sc.find_first_not_of("0");
size_t pl0 = sc.find_last_not_of("0");
std::string sc_trim = sc.substr(pf0, pl0 - pf0 + 1);
std::cout << "sc_trim is " << sc_trim << std::endl;
// DW: move "." according to e
size_t pp = sc_trim.find_first_of(".");
if (pp == std::string::npos) {
// DW: this means there is no "."
pp = 0;
}
int pp_tobe = pp + de;
sc_trim.erase(pp, 1);
if (de > 0) {
// DW: should move "." to the right
if (pp_tobe < sc_trim.size()) {
sc_trim.insert(pp_tobe, ".");
} else {
sc_trim += std::string(pp_tobe - sc_trim.size() + 1, '0');
sc_trim += ".0";
}
} else {
// DW: should move "." to the left
if (pp_tobe >= 0) {
sc_trim.insert(pp_tobe, ".");
} else {
sc_trim = "." + std::string(0 - pp_tobe, '0') + sc_trim;
}
}
if (sc_trim == ".") {
// DW: this means sc_trim is actually 0 now
sc_trim = "0.0";
}
return sc_trim;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다