CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983

    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
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

  2. #2
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983
    somebody ?
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

  3. #3
    Join Date
    Jun 2001
    Location
    Wuerzburg, Germany
    Posts
    90
    I think you have to do it manualy. There is no format function for the currency format like in Java.

    Michael

  4. #4
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983
    I dont have any idea on how to do that manually. Can you tell me how ?
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

  5. #5
    Join Date
    Jun 2001
    Location
    Wuerzburg, Germany
    Posts
    90
    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;
    }
    }

  6. #6
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983
    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
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

  7. #7
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275
    (from MSDN)

    strtod, wcstod
    Convert strings to a double-precision value.

    double strtod( const char *nptr, char **endptr );

  8. #8
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983
    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 ?
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

  9. #9
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983
    Anybody ??
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

  10. #10
    Join Date
    Mar 2002
    Location
    Croatia
    Posts
    275
    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;
    }

  11. #11
    Join Date
    Jun 2001
    Location
    Michigan
    Posts
    2,222
    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++.

  12. #12
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983
    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.
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

  13. #13
    Join Date
    Nov 2003
    Posts
    1
    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();

  14. #14
    Join Date
    Dec 2001
    Location
    Canada/Montreal
    Posts
    983
    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.
    Sonu [MVP, MCAD.NET]
    Website: http://DotNetSlackers.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured