Click here to Skip to main content
15,915,599 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi. I just want to set this nubmer in this format:

The Number is this : 123456789.98765432123456789

Want like this format. 123,456,789.98765432123456789

VB
TextBox1.Text = (Format((CDec(TextBox1.Text)), "#,#.####"))


I use this code for thousand separator but Decimal Digit Counts always changes.

(Decimal numbers will keep as original. Decimal numbers Will not round up. There are 17 digits in Decimal. But sometimes 5 sometimes 8 Sometims 3. How can I arrange thousand seperated and keep all decimal digits (keep original digit counts).

for example:

if we preset 5 digits for decimal number and if decimal .23 then result will be .23000
this is not I want

I want to decimal if .23 then retun .23, if decimal ,234 then return ,234 not .23400

advanced thanks.

What I have tried:

VB
TextBox1.Text = (Format((CDec(TextBox1.Text)), "#,#.####"))
Posted
Updated 2-Jan-19 6:02am

Perhaps something like
VB
Dim formatting As String = "###,###,##0.0####"

Debug.WriteLine(0.23.ToString(formatting))
Debug.WriteLine(0.234.ToString(formatting))
Debug.WriteLine(1234.23.ToString(formatting))
Debug.WriteLine(123456789.234.ToString(formatting))

The result would be
0.23
0.234
1,234.23
123,456,789.234
 
Share this answer
 
See Standard Numeric Format Strings | Microsoft Docs[^], for usage with the 'N' format specifier, and associated options.
 
Share this answer
 
Comments
Wendelius 2-Jan-19 11:29am    
That's what I also thought first, but N uses standard amount of decimals, typically 2...
Richard MacCutchan 2-Jan-19 11:38am    
You can modify that with the precision value, or options.
Wendelius 2-Jan-19 11:43am    
But wouldn't you somehow have to know the number of desired decimals. For example


Dim formatting As String = "N3"

Debug.WriteLine(0.23.ToString(formatting))
Debug.WriteLine(0.234.ToString(formatting))
Debug.WriteLine(1234.23.ToString(formatting))
Debug.WriteLine(123456789.234.ToString(formatting))

gives

0.230
0.234
1,234.230
123,456,789.234
Richard MacCutchan 2-Jan-19 11:47am    
Not necessarily, you just need to specify the maximum that you want displayed, where the default is 2.
Quote:
How can I set number with thousand separator and with unlimited decimal ?

If you don't find the magic formatting string that fulfill your needs, may be you can do it yourself with little programming.
Your problem is decimal part, just remove it from formatting.
1) split string at decimal point
2) format integer part as you want
3) concatenate both parts
 
Share this answer
 
Comments
Richard MacCutchan 2-Jan-19 12:04pm    
See my solution, which shows how .NET provides this feature.
Patrice T 2-Jan-19 12:10pm    
I know, mine is just another approach.
Richard MacCutchan 2-Jan-19 12:19pm    
But why would anyone want to do it that way?
Patrice T 2-Jan-19 12:30pm    
One that don't use .net or don't know your solution.
in fact I separated decimal part and main number part then after thousand separeted I get together them again. it work great but I asked if there is easy simple and productive solution.
 
Share this answer
 
Comments
Richard MacCutchan 2-Jan-19 15:45pm    
Yes, a very easy answer, as listed in my Solution above. Don't try to re-invent the wheel when .NET is full of useful time saving classes and methods; make use of the MSDN documentation to get familiar with them.
Nicomendox 3-Jan-19 3:31am    
Thanks again. but I want to ask another question.

for excample when I multiply 1.23456789012 with 1.5

the code is :

TextBox3.Text = (Format((CDec(TextBox1.Text)) * (CDec(TextBox2.Text)), "###,###,##0.############"))

With 12 digits for Decimal Numbers then result is 1.419753073638

When I change 11 digits for Decimal then result is 1,41975307364

İts rounding up to last 8 and left side 3 became 4

How can I arrange to Decimals without rounding up or dawn ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900