*Hi,

Greetings to u all ...
I am new to this website as well as in C++.
I got a question in c++.
I need to remove decimal point from amount (declared as CString)
Eg. Amt is 145.76
I need the final output as 014576 and same to be stored in the buffer.
ie. Amt has to be in 6 digits length.
What is the procedure to convert to reqd. output.
If the total digit is < 6, pad with 0
Hope i have made clear
Thanks

Anu
*

Dani AI

Generated

A deterministic string/fixed‑point approach is safest here. ’s numeric idea (multiply and shift) will work for many inputs and confirmed it, but floating point can introduce rounding surprises and locale issues (decimal comma, thousands separators). The robust flow: trim and remove separators, split at the decimal point, take exactly two fractional digits (pad or truncate as your policy dictates), concatenate whole+fraction, then left‑pad to 6 digits (or handle overflow explicitly).

Example implementation using MFC CString:

#include <tchar.h> // _istdigit/_T
#include <afx.h>   // CString

CString AmountToSixDigits(CString s)
{
    s.Trim();
    s.Replace(_T(","), _T("")); // remove thousands separators
    bool negative = (s.GetLength() > 0 && s[0] == _T('-'));
    if (negative) s = s.Mid(1);

    int p = s.Find(_T('.'));
    CString whole = (p >= 0) ? s.Left(p) : s;
    CString frac  = (p >= 0) ? s.Mid(p + 1) : _T("");

    CString wdigits, fdigits;
    for (int i = 0; i < whole.GetLength(); ++i)
        if (_istdigit(whole[i])) wdigits += whole[i];

    for (int i = 0; i < frac.GetLength() && fdigits.GetLength() < 2; ++i)
        if (_istdigit(frac[i])) fdigits += frac[i];

    while (fdigits.GetLength() < 2) fdigits += _T('0');

    CString combined = wdigits + fdigits;

    while (combined.GetLength() < 6) combined = CString(_T("0")) + combined;

    if (combined.GetLength() > 6) {
        // either signal an error or choose a policy (here we keep rightmost 6 digits)
        combined = combined.Right(6);
    }

    if (negative) combined = CString(_T("-")) + combined;
    return combined;
}

Notes and gotchas: decide whether to truncate or round when more than two decimals (string method above truncates). If you already have a numeric value, lround(value * 100.0) plus CString::Format(_T("%06ld"), cents) is convenient but be mindful of floating‑point precision (std::round). For CString details see the Microsoft docs: . When writing into a raw char buffer, allocate space for the null terminator (at least 7 bytes for six digits).

Recommended Answers

All 2 Replies

So you can do it by num(mod1) and as long as it's not 0 multiply by 10?
Good luck on that !

Thanks Despairy,logic worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.