# function to convert a number to a character

Hello everyone
Xojo

I am grateful for the interest and scope of the following:

In a desktop system, I have a calculation with a numerical value

and for some reason I have to convert it to character, to save it in my Mysql database, for example:

Dim quantity = 4211750882.72
Dim sql = “INSERT tasacion (avaluo) VALUES (’” + str (quantity) + "’)

What is the most efficient function to convert a number to a character

is str?

Cordially,
Raul Juarez Pulache

You are needing to convert it to a string because you are creating an SQL query AS A STRING
(using prepared statements would be a bit different, as you numeric values could stay numeric)

And either STR(), FORMAT() or CSTR() depending on your requirements

Ideally, have a numeric field defined and as Dave says, send a number as a parameterised query

If you must turn it into a string before storing,

Why?

If you use CSTR() and the app is used in both the USA and in France, then what is written to the database will differ.
USA will get 4211750882.72
France will get 4211750882,72

(And possibly a few more commas and points along the way at the thousand mark)

When you read that back, if you try to do maths with it, it may fail to parse properly.

Using STR will always do it the American Way.
Getting the number back as a number … use VAL() which expects the American Format

For data storage, use Str() and Val(). For display and user input, use CStr() (or Format) and CDbl().

i almost always use;

``````Dim quantity = 4211750882.72
Dim sql = "INSERT tasacion (avaluo) VALUES ('" + quantity.ToText + "')``````

You can even parse a Format in the Integer.ToText function and the Locale of the system/user.
Integer reference

The same rules apply for ToText. For data storage use Local.Raw. For display/input, use Local.Current.

Or, as Dave mentioned, use prepared statements and don’t worry about what to use when. You leave it as an number and let the database do what is right.

Thank you so much
Dave, Jeff, Tim, Thom

Your valuable help was very important, to solve the required

Blessings

Raul Juarez Pulache