Пример #1
0
//static
std::string OTAssetContract::formatLongAmount(int64_t & lOriginalValue, int32_t nFactor/*=100*/, int32_t nPower/*=2*/, const char * szSymbol/*=""*/,
                                              const char * szSeparator/*=","*/, const char * szDecimalPoint/*="."*/)
{
    std::stringstream sss;
    OTString strRemainder;
    // --------------------------------------------------
    // If the original value is 0, we still want to format the
    // string properly for a 0 value. (And then return.)
    //
    if (0 == lOriginalValue)
    {
        sss << szSymbol << " "; // Currency symbol
        
        if (!(nFactor < 2))
        {
            sss << szDecimalPoint;
            
            strRemainder.Format("%0*ld", nPower, 0);
        }
        else
            strRemainder.Format("%lld", 0);
        
        sss << strRemainder.Get();
        return sss.str();
    }
    // --------------------------------------------------
    int64_t lAbsoluteValue = (lOriginalValue > 0) ? lOriginalValue : (lOriginalValue * (-1));
    // --------------------------------------------------    
    int64_t lValue     = lAbsoluteValue / nFactor; // For example, if 506 is supposed to be $5.06, then dividing 506 by factor of 100 results in 5 dollars.
    int64_t lRemainder = lAbsoluteValue % nFactor; // For example, if 506 is supposed to be $5.06, then 506 mod 100 results in 6 cents.
    
    if (nFactor < 2) // Basically, if nFactor is 1.
        strRemainder.Set("");
    else
        strRemainder.Format("%0*ld", nPower, lRemainder); // If remainder is 6 (cents) and nPower is 2, strRemainder gets set here to 06.    
    // ------------------------------------------------------
    // Here we add the negative sign, if the value itself is negative.
    //
    if (lOriginalValue < 0)
    {
//        const std::moneypunct<char, false> &mp = std::use_facet< std::moneypunct<char, false> >(std::locale ());
//        sss << mp.negative_sign();
     
        // For some reason the above code isn't working, so I've got the negative sign
        // hardcoded here to '-'.
        //
        sss << "-";
    }
    // ------------------------------------------------------    
    // Here we add the currency symbol.
    //
    sss << szSymbol << " "; // Currency symbol
    // ------------------------------------------------------
    OTString strValue;
    strValue.Format("%lld", lValue);
    // ---------------------------------
    char     cTemp = '\0';
    uint32_t uValueStrLength = strValue.GetLength();
    // ---------------------------------
    // Here we add the main body of the amount, including separators (commas.)
    //
    while (uValueStrLength > 0)
    {
        cTemp = strValue.sgetc();
        
        sss << cTemp;
        
        --uValueStrLength;
        
        if ((uValueStrLength > 0) && (0 == (uValueStrLength % 3)))
            sss << szSeparator;
    }
    // ------------------------------------------------------
    // Here we deal with the decimal point, etc.
    //
    if (!(nFactor < 2))
    {
        sss << szDecimalPoint;
        // -----------------------------
        sss << strRemainder.Get();
    }
    // -----------------------------
    return sss.str();
}