-
November 21st, 2003, 04:59 AM
#1
How should I read froma dbase ?
Hi guys,
I am reading some data from a database. The data is stored as a currency and I am reading the currency. The data read from the dbase need to be stored in a list control, so I store the data in a CString.
But the data is display as following:
1000000.0000
and not as
1.000.000,00
How can I store the value in the correct form ?
Thanks, Sonu
-
November 21st, 2003, 06:02 AM
#2
-
November 21st, 2003, 06:19 AM
#3
I think you have to do it manualy. There is no format function for the currency format like in Java.
Michael
-
November 21st, 2003, 06:30 AM
#4
I dont have any idea on how to do that manually. Can you tell me how ?
-
November 21st, 2003, 06:32 AM
#5
bool BFormat:oubleToString(const double _CdWert, CString &_rstrDoubleString,
const int _CiAnzNachKommaStell, bool _bTausenderzeichen,
bool _bExponent, bool _bVorzeichPlus)
{
// Wert formatieren
try
{
CString format;
format.Format("%%.%df", _CiAnzNachKommaStell);
_rstrDoubleString.Format(format, _CdWert);
_rstrDoubleString.Replace('.', GetDezimalzeichen());
}
catch(...)
{
return false;
}
// TODO: was ist, wenn man exponentschreibweise reinmacht in ff Fkt?
// Tausenderzeichen einfügen, falls gewünscht
if(_bTausenderzeichen)
InsertTausenderZeichen(_rstrDoubleString, _CiAnzNachKommaStell);
// Vorzeichen '+' bei positiven Werten dazu, falls gewünscht
if(_bVorzeichPlus && (_CdWert >= 0))
_rstrDoubleString.Insert(0, '+');
return true;
}
// =============================================================================
// Methode: BFormat::InsertTausenderZeichen
//
// Beschreibung: Fügt in übergebenen Wert (als String übergeben) Tausendertrenner
// ein. Ist der Wert 0, so wird zB 0,00 erzeugt, je nach übergebener
// Anzahl der Nachkommastellen.
// Hinweis: der Wert 0 ist in allen Varianten erlaubt.
// zB 0.000,0000: wird in 0,00 umgewandelt, wenn _Ci NachkommaStellen = 2.
//
// Parameter: CString &_rstrWert - Wert
// const int _CiNachkommaStellen - Anzahl der Nachkommastellen des
// übergebenen Wertes
//
// Rückgabewert: void
// =============================================================================
void BFormat::InsertTausenderZeichen(CString &_rstrWert, const int _CiNachkommaStellen)
{
// Tausenderzeichen entfernen
_rstrWert.Remove(GetTausenderzeichen());
// '0' ist in allen Varianten erlaubt und diese Funktion kann verlassen werden
if(IsWertNull(_rstrWert))
{
CreateFormattedNull(_rstrWert, _CiNachkommaStellen);
return;
}
// Führende Nullen entfernen
RemoveFuehrendeNullen(_rstrWert);
// Variablen für Ermittlung des Vorkommateils ohne Vorzeichen
int iStelleTrenner = 0; // Bei Ganzzahlen braucht man keine Stelle für
if(_CiNachkommaStellen > 0) // den Dezimaltrenner
iStelleTrenner = 1; // Bei Fließkommazahlen braucht man 1 Stelle für den Trenner
int iStelleVorzeich = 0; // Vorzeichen, falls vorhanden, braucht 1 Stelle
if(_rstrWert.GetAt(0) == '+' || _rstrWert.GetAt(0) == '-')
iStelleVorzeich = 1;
// Anhand Vorkommastellen, maximale Anzahl der Tausenderpunkte ermitteln
int iVKStellen = _rstrWert.GetLength() - _CiNachkommaStellen - iStelleTrenner - iStelleVorzeich;
unsigned short usMaxAnzPkte;
if(iVKStellen%3 == 0)//"123" hat 3 Stellen und durch 3 ergibt dies 1 Tausenderzeichen, das darf nicht sein!
usMaxAnzPkte = iVKStellen/3 - 1;
else
usMaxAnzPkte = iVKStellen/3;
// Tausenderzeichen neu einfügen
unsigned short usAnzPkte = 0;
for( int i = 0; i < usMaxAnzPkte; ++i)
{
// Hole Vorkommateil des Strings mit Vorzeichen: bei Fliesskommazahlen 1 Stelle
// für den Dezimaltrenner und die Nachkommastellen abziehen
int iAnzVKStellen = _rstrWert.GetLength() - iStelleTrenner - _CiNachkommaStellen;
// Füge Tausenderpunkt ein
_rstrWert.Insert(iAnzVKStellen - ((i+1)*3 + i), GetTausenderzeichen());
++usAnzPkte;
}
}
-
November 21st, 2003, 07:07 AM
#6
The problem is that if I retrieve the value with the function:
recordset.GetFieldValue(10,cstring);
The value is stored directly as a string. How do I get the value in double ?
Sonu
-
November 21st, 2003, 07:37 AM
#7
(from MSDN)
strtod, wcstod
Convert strings to a double-precision value.
double strtod( const char *nptr, char **endptr );
-
November 21st, 2003, 08:11 AM
#8
ergas,
thanks for the function. But that doesnt solve my prob !! Now I have also tried it like this:
Code:
CDBVariant varValue;
recordset.GetFieldValue(10,varValue);
TRACE("%d",varValue.m_dblVal);
// Database value:1.000.000,00 €
// Returned value: 12938672
Can somebody give me a hint ?
-
November 21st, 2003, 08:26 AM
#9
-
November 21st, 2003, 09:09 AM
#10
Here is how read int from the database.
Maybe you can adopt it for double?
int CIERecordset::GetFldInt( CString fldName )
{
COleVariant fld;
fld.ChangeType( VT_INT, NULL);
m_pRS->GetFieldValue( fldName, fld );
return (int)fld.pbstrVal;
}
-
November 21st, 2003, 09:27 AM
#11
Try to look up the type for your CDBVariant variable by look at the CDBVariant::m_dwType member.
The following is a description of this type:
m_dwType Union data member
DBVT_NULL No union member is valid for access.
DBVT_BOOL m_boolVal
DBVT_UCHAR m_chVal
DBVT_SHORT m_iVal
DBVT_LONG m_lVal
DBVT_SINGLE m_fltVal
DBVT_DOUBLE m_dblVal
DBVT_DATE m_pdate
DBVT_STRING m_pstring
DBVT_BINARY m_pbinary
If you are attempting to access the m_dblVal member, and it is not a DBVT_DOUBLE type, that would be a clue as to why the following doesn't work:
Code:
CDBVariant varValue;
recordset.GetFieldValue(10,varValue);
TRACE("%d",varValue.m_dblVal);
// Database value:1.000.000,00 €
// Returned value: 12938672
Can you let us know what type it is returning? Also, have you tried to use the strtod function as suggested by ergas to convert your string value to a double?
There is also a CURRENCY datatype in vc++.
-
November 21st, 2003, 10:00 AM
#12
Thanks guys, but I got the solution ( thanks to YvesM). The solution is to use the function GetCurrencyFormat();
Here is how it looks like:
Code:
TCHAR* CMyClass::ConvertStringToCurrency(CString csString)
{
CString input = csString;
int len = GetCurrencyFormat(LOCALE_USER_DEFAULT, 0, input, 0, 0, 0);
TCHAR *buffer = new TCHAR[len + 1];
memset(buffer, 0, sizeof(TCHAR) * (len + 1));
GetCurrencyFormat(LOCALE_USER_DEFAULT, 0, input, 0, buffer, len + 1);
return buffer;
}
Thanks to all.
-
November 28th, 2003, 02:50 PM
#13
COleVariant v;
recordset.GetFieldValue(10, v);
COleCurrency c(v);
CString strReturnVal = c.Format();
This is correct way to read currency from database and to convert it to string. If you need other locale then user default, take a look on parameters for COleCurrency::Format();
-
December 1st, 2003, 12:29 AM
#14
Originally posted by sslavko
Code:
COleVariant v;
recordset.GetFieldValue(10, v);
COleCurrency c(v);
CString strReturnVal = c.Format();
This is correct way to read currency from database and to convert it to string. If you need other locale then user default, take a look on parameters for COleCurrency::Format();
Unfortunaly the CRecordSet::GetFieldValue() does not take a COleVariant object as a param. If I am not wrong this will only work if you use the class CDaoRecordset.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|