Format integer with ToString("D2") in SqLite

titojd

Well-known member
Joined
Oct 12, 2018
Messages
57
Programming Experience
1-3
I'm trying to save my list of numbers with a Zero to the left of the numbers <=9 and it's not having any effect,
I'm using the following code in Insert:

C#:
cmd.Parameters.AddWithValue("@_01", número._01.ToString("D2"));

I have the same code on Sql server and it works perfect,
would there be another way to do this in SqLite?
 
Solution
Is the column in SQLite definitely a string? If it's a number then you should just write it as a number and instead format it when you come to use it
Is the column in SQLite definitely a string? If it's a number then you should just write it as a number and instead format it when you come to use it
 
Solution
Note that, if you only want two decimal places in the data you save to the database, you can round your values before saving. They will remain numbers the whole time though, as is appropriate for numeric data.
 
my array generates Prime numbers with zero on the left EX: 01, 02, 05, but in the Bank it saves EX: 1, 2, 5, but I already managed to solve that, Thanks
 
If you made the schema for your table to store integers, that is expected. Recall that values are different from presentation of the values. For example, the number 12 in decimal, is C in hexadecimal, and 1100 in binary, and 14 in octal.
 
Note that, if you only want two decimal places in the data you save to the database, you can round your values before saving. They will remain numbers the whole time though, as is appropriate for numeric data.

Sorry, ignore my previous post. I was thinking of decimal places, not zero-padding. I mistook your format specifier for "N2" or "F2". Rounding is not relevant in this case.

As suggested though, you should not be trying to store zero-padding in the database. Just store the numbers. If you want to zero-pad those numbers when you display them to the user, do it then. You have to convert the numbers to text for display anyway so that is when you add any text-based formatting.
 
Back
Top Bottom