п»ї#include "sqlite3ext.h"#include <stdlib.h>#include <stdio.h>SQLITE_EXTENSION_INIT1static void moneyFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ char *result; int sum = sqlite3_value_int(argv[0]); int full = sum / 100; int dec = sum % 100; if (dec == 0){ snprintf(result, 8, "%d,00", full); } else if (dec > 0 && dec < 10){ snprintf(result, 8, "%d,0%d", full, dec); } else if (dec >= 10 && dec <= 99){ snprintf(result, 8, "%d,%d", full, dec); } else return; sqlite3_result_text(context, result, 8, 0);}int sqlite3_extension_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ SQLITE_EXTENSION_INIT2(pApi) sqlite3_create_function(db, "money", 1, SQLITE_ANY, 0, moneyFunc, 0, 0); return 0;}
SELECT money(100)
SELECT money(100), money(350)
#include "sqlite3ext.h"#include <stdlib.h>#include <stdio.h>SQLITE_EXTENSION_INIT1static void moneyFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ char *result = (char *) malloc (sizeof(char)*(8)); int sum = sqlite3_value_int(argv[0]); int full = sum / 100; int dec = sum % 100; if (dec == 0){ snprintf(result, 8, "%d,00", full); } else if (dec > 0 && dec < 10){ snprintf(result, 8, "%d,0%d", full, dec); } else if (dec >= 10 && dec <= 99){ snprintf(result, 8, "%d,%d", full, dec); } else return; sqlite3_result_text(context, result, 8, 0);}int sqlite3_extension_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ SQLITE_EXTENSION_INIT2(pApi) sqlite3_create_function(db, "money", 1, SQLITE_ANY, 0, moneyFunc, 0, 0); return 0;}
#include <QString>#include <QObject>#include <sqlite3.h>static void moneyFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ int sum = sqlite3_value_int(argv[0]); int full = sum / 100; int dec = sum % 100; QString resultStr; resultStr.clear(); resultStr = QString(QObject::tr("0,00")); if (dec == 0){ resultStr = QString(QObject::tr("%1,00")).arg(full); } else if (dec > 0 && dec < 10){ resultStr = QString(QObject::tr("%1,0%2")).arg(full).arg(dec); } else if (dec >= 10 && dec <= 99){ resultStr = QString(QObject::tr("%1,%2")).arg(full).arg(dec); } sqlite3_result_text(context, resultStr.toAscii().data(), resultStr.size(), 0);}