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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
Copyright (C) 2000, 2001, 2002 RiskMap srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it under the
terms of the QuantLib license. You should have received a copy of the
license along with this program; if not, please email ferdinando@ametrano.net
The license is also available online at http://quantlib.org/html/license.html
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file dataformatters.cpp
\brief classes used to format data for output
\fullpath
ql/%dataformatters.cpp
*/
// $Id: dataformatters.cpp,v 1.7 2002/01/16 14:43:47 nando Exp $
#include <ql/dataformatters.hpp>
#include <ql/null.hpp>
namespace QuantLib {
std::string IntegerFormatter::toString(int i, int digits) {
if (i == Null<int>())
return std::string("null");
char s[64];
QL_SPRINTF(s,"%*d",(digits>64?64:digits),i);
return std::string(s);
}
std::string DoubleFormatter::toString(double x, int precision, int digits) {
if (x == Null<double>())
return std::string("null");
char s[64];
QL_SPRINTF(s,"%*.*f",(digits>64?64:digits),
(precision>64?64:precision),x);
return std::string(s);
}
std::string EuroFormatter::toString(double amount) {
std::string output;
if (amount < 0.0) {
output = "-";
amount = -amount;
} else {
output = " ";
}
int triples = 0;
while (amount >= 1000.0) {
amount /= 1000;
triples++;
}
output += IntegerFormatter::toString(int(amount));
amount -= int(amount);
while (triples > 0) {
amount *= 1000;
output += ","+IntegerFormatter::toString(int(amount),3);
amount -= int(amount);
triples--;
}
amount *= 100;
output += "."+IntegerFormatter::toString(int(amount+0.5),2);
return output;
}
std::string RateFormatter::toString(double rate, int precision) {
return DoubleFormatter::toString(rate*100,precision)+"%";
}
std::string DateFormatter::toString(const Date& d, bool shortFormat) {
static const std::string monthName[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" };
std::string output;
if (d == Date()) {
output = "Null date";
} else {
int dd = d.dayOfMonth(), mm = int(d.month()), yyyy = d.year();
if (shortFormat) {
output = (mm < 10 ? "0" : "") + IntegerFormatter::toString(mm);
output += (dd < 10 ? "/0" : "/") +
IntegerFormatter::toString(dd);
output += "/" + IntegerFormatter::toString(yyyy);
} else {
output = monthName[mm-1] + " ";
output += IntegerFormatter::toString(dd);
switch (dd) {
case 1:
case 21:
case 31:
output += "st, ";
break;
case 2:
case 22:
output += "nd, ";
break;
case 3:
case 23:
output += "rd, ";
break;
default:
output += "th, ";
}
output += IntegerFormatter::toString(yyyy);
}
}
return output;
}
std::string CurrencyFormatter::toString(Currency c) {
switch (c) {
case EUR: return "EUR";
case GBP: return "GBP";
case USD: return "USD";
case DEM: return "DEM";
case ITL: return "ITL";
case CHF: return "CHF";
case AUD: return "AUD";
case CAD: return "CAD";
case DKK: return "DKK";
case JPY: return "JPY";
case PLZ: return "PLZ";
case SEK: return "SEK";
case CZK: return "CZK";
case EEK: return "EEK";
case ISK: return "ISK";
case NOK: return "NOK";
case SKK: return "SKK";
case HKD: return "HKD";
case NZD: return "NZD";
case SGD: return "SGD";
case GRD: return "GRD";
case HUF: return "HUF";
case LVL: return "LVL";
case ROL: return "ROL";
case BGL: return "BGL";
case CYP: return "CYP";
case LTL: return "LTL";
case MTL: return "MTL";
case TRL: return "TRL";
case ZAR: return "ZAR";
case SIT: return "SIT";
case KRW: return "KRW";
default: return "unknown";
}
}
std::string StringFormatter::toLowercase(const std::string& s) {
std::string output = s;
for (std::string::iterator i=output.begin(); i!=output.end(); i++)
*i = QL_TOLOWER(*i);
return output;
}
std::string StringFormatter::toUppercase(const std::string& s) {
std::string output = s;
for (std::string::iterator i=output.begin(); i!=output.end(); i++)
*i = QL_TOUPPER(*i);
return output;
}
}
|