Created functions for formatting floating point numbers in a non-scientific way.

This commit is contained in:
Moritz Bunkus 2009-05-07 08:18:35 +02:00
parent a675be260f
commit 52f46ed143
3 changed files with 58 additions and 22 deletions

View File

@ -40,8 +40,56 @@ format_timecode(int64_t timecode,
}
std::string
to_string(int64_t i) {
return (boost::format("%1%") % i).str();
to_string(int64_t value) {
return (boost::format("%1%") % value).str();
}
std::string
to_string(int value) {
return (boost::format("%1%") % value).str();
}
std::string
to_string(uint64_t value) {
return (boost::format("%1%") % value).str();
}
std::string
to_string(unsigned int value) {
return (boost::format("%1%") % value).str();
}
std::string
to_string(double value,
unsigned int precision) {
int64_t scale = 1;
for (int i = 0; i < precision; ++i)
scale *= 10;
return to_string((int64_t)(value * scale), scale, precision);
}
std::string
to_string(int64_t numerator,
int64_t denominator,
unsigned int precision) {
std::string output = (boost::format("%1%") % (numerator / denominator)).str();
int64_t fractional_part = numerator % denominator;
if (0 != fractional_part) {
std::string format = (boost::format(".%%0%1%d") % precision).str();
output += (boost::format(format) % fractional_part).str();
std::string::iterator end = output.end() - 1;
while (*end == '0')
--end;
if (*end == '.')
--end;
output.erase(end + 1, output.end());
}
return output;
}
void

View File

@ -36,6 +36,11 @@ std::string MTX_DLL_API format_timecode(int64_t timecode, unsigned int precision
void MTX_DLL_API fix_format(const char *fmt, std::string &new_fmt);
std::string MTX_DLL_API to_string(int64_t i);
std::string MTX_DLL_API to_string(int value);
std::string MTX_DLL_API to_string(unsigned int value);
std::string MTX_DLL_API to_string(int64_t value);
std::string MTX_DLL_API to_string(uint64_t value);
std::string MTX_DLL_API to_string(double value, unsigned int precision);
std::string MTX_DLL_API to_string(int64_t numerator, int64_t denominator, unsigned int precision);
#endif // __MTX_COMMON_STRING_FORMATTING_H

View File

@ -37,6 +37,7 @@
#include "common/ebml.h"
#include "common/matroska.h"
#include "common/mm_io.h"
#include "common/string_formatting.h"
#include "extract/mkvextract.h"
#include "extract/xtr_base.h"
@ -57,24 +58,6 @@ static vector<timecode_extractor_t> timecode_extractors;
// ------------------------------------------------------------------------
static std::string
format_timecode_for_timecode_file(int64_t timecode) {
std::string output = (boost::format("%1%") % (int64_t)(timecode / 1000000)).str();
int64_t fractional_part = timecode % 1000000;
if (0 != fractional_part) {
output += (boost::format(".%06d") % fractional_part).str();
std::string::iterator zeroes = output.end() - 1;
while (*zeroes == '0')
--zeroes;
output.erase(zeroes + 1, output.end());
}
return output;
}
static void
close_timecode_files() {
vector<timecode_extractor_t>::iterator extractor;
@ -85,7 +68,7 @@ close_timecode_files() {
sort(timecodes.begin(), timecodes.end());
mxforeach(timecode, timecodes)
extractor->m_file->puts(boost::format("%1%\n") % format_timecode_for_timecode_file(*timecode));
extractor->m_file->puts(to_string(*timecode, 1000000, 6) + "\n");
delete extractor->m_file;
}
timecode_extractors.clear();