File: ConversionUtils.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (52 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "ConversionUtils.h"
#include "someUtil.h"
#include "errorMsg.h"

#include <cmath>

using namespace std;

void appendIntToString (string& ioString, const int inValue) {
	std::ostringstream o;
	o << ioString<< inValue;
	ioString = o.str();
}

string appendInt2string(const int x)
{
	string res;
	appendIntToString(res, x);
	return res;
}

string appendDouble2string(const double x, const int lenght){
	
	// first getting the integer part:
	int theIntegerPart = static_cast<int>(x);
	double theRemainingPart = fabs(x-theIntegerPart);
	int integerRepresentingTheRemainingPart = static_cast<int>(theRemainingPart*pow(10.0,lenght));
	string part1, part2; 
	appendIntToString(part1, theIntegerPart);
	appendIntToString(part2, integerRepresentingTheRemainingPart);
	while (part2.length()<lenght){
		part2.insert(0, "0");
	}

	string result = part1;
	result += ".";
	result += part2;

	// removing 0 from the end
	int i = result.length()-1;
	while (result[i]!='.' && i>0 && result[i]=='0'){
		result.erase(i);
		i--;
	}
	
	// removing "." if this is the last character in the string.
	if (result[result.length()-1]=='.')
	result.erase(result.length()-1);

	return result;
}