# How Can I Format Decimal Number ?

Page 1 of 1

## 6 Replies - 308 Views - Last Post: 21 December 2018 - 07:16 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=413909&amp;s=eaa5f7264db2e187198c61be597fc887&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Nicomendox

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 05-March 16

# How Can I Format Decimal Number ?

Posted 02 December 2018 - 03:42 PM

```TextBoxOprtr1.Text = FormatNumber((TextBoxOprtr1.Text).ToString, 2)
TextBoxOprtr1.Text = Format((TextBoxOprtr1.Text) * 1, "#,#.##")
```

For Formated Decimal number I use both uper codes. But both codes always round to for last 2 decimal number.

For example The number is 1234567,987654321

I want to make it like = 1,234,567.987654321 dont round decimal upper. (dont want this 1,234,567.99)

I want automaticly keep all decimal numbers. sometimes 2 sometimes 4 sometimes 6 digits for decimal.

how can I do it ?

Is This A Good Question/Topic? 0

## Replies To: How Can I Format Decimal Number ?

### #2 maceysoftware

• Foo

Reputation: 367
• Posts: 1,569
• Joined: 07-September 13

## Re: How Can I Format Decimal Number ?

Posted 03 December 2018 - 01:29 AM

Well, you're telling it to go to 2 decimal places, therefore, it is going to 2 decimal places.

See the 2 after the comma, you're saying 2 decimal places.
```TextBoxOprtr1.Text = FormatNumber((TextBoxOprtr1.Text).ToString, 2)
```

See the dot and then two '##' you're saying 2 decimal places.
```TextBoxOprtr1.Text = Format((TextBoxOprtr1.Text) * 1, "#,#.##")
```

I actually dislike the format routines and prefer to use the .ToString("N2") method, so:
```TextBoxOprtr1.Text = (TextBoxOprtr1.Text * 1).ToString("N2")
```

However, all of these methods will format the result to 2 decimal place, you can switch it out to 4 decimal places, by replacing the 2's with 4's or by changing the number of hashes depending on the method you want to use. you just need to format it correctly depending on your situation.

### #3 andrewsw

• dependency injected

Reputation: 6668
• Posts: 27,308
• Joined: 12-December 12

## Re: How Can I Format Decimal Number ?

Posted 03 December 2018 - 01:41 AM

If a value in a control should be a number then you should first check that it is a valid number and convert its value to a number for further processing (perhaps using a numeric updown control).

Number formatting always rounds; to truncate the value you can use Math.Truncate() such as Math.Truncation(value * 100) / 100. Or you can take the string representation and extract the characters you need using Substring etc..

### #4 Nicomendox

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 05-March 16

## Re: How Can I Format Decimal Number ?

Posted 03 December 2018 - 08:34 AM

Maybe I couldnt explain what is main problem for me.

the number is this : 123456789.987654321

How can make this format (only main part of number) 123,456,789.987654321

I mean to dont touch decimal numbers XXX,XXX,XXX.987654321

I tought to put comma every 3 digits I found this code but not work in all number.length and also with decimal number

```Dim Number As String = TextBoxOprtr1.Text         If Number.ToString.Length = 4 Then             Dim FirstDigit As String = Mid(Number, 1, 1)             Dim RemainingDigits As String = Mid(Number, 2, 3)             TextBoxOprtr2.Text = FirstDigit & "," & RemainingDigits 'Data update statement goes here'         End If         If Number.ToString.Length = 5 Then             Dim FirstDigits As String = Mid(Number, 1, 2)             Dim RemainingDigits As String = Mid(Number, 3, 3)             TextBoxOprtr2.Text = FirstDigits & "," & RemainingDigits         End If          If Number.ToString.Length = 6 Then             Dim FirstDigits As String = Mid(Number, 1, 3)             Dim RemainingDigits As String = Mid(Number, 4, 3)             TextBoxOprtr2.Text = FirstDigits & "," & RemainingDigits         End If         If Number.ToString.Length = 7 Then             Dim FirstDigits As String = Mid(Number, 1, 1)             Dim NextDigits As String = Mid(Number, 2, 3)             Dim RemainingDigits As String = Mid(Number, 5, 3)             TextBoxOprtr2.Text = FirstDigits & "," & NextDigits & "," & RemainingDigits         End If
```

if number has 9 digits with decimal numbers not working.

sorry wrong type code added how can I edit my post ?

### #5 dbasnett

Reputation: 121
• Posts: 671
• Joined: 01-October 08

## Re: How Can I Format Decimal Number ?

Posted 03 December 2018 - 11:00 AM

Here is a hint using Standard Numeric Format Strings

```        Dim d As Decimal = 123456789.987654321D
Dim s As String = d.ToString("n9")

```

### #6 maceysoftware

• Foo

Reputation: 367
• Posts: 1,569
• Joined: 07-September 13

## Re: How Can I Format Decimal Number ?

Posted 04 December 2018 - 01:46 AM

Myself and dbasnett have pointed you towards .ToString({Format}) and passing through the decimal precision you want to format too.

I even told you how to do it using your Format Functions, so before I stick my hat in the ring again to help you can you explain why this is not helping you, the methods we have given you does work:

I have hidden the code on purpose, however, you can see I am calling a function call FormatDecimalValue, this is my own format which uses the .ToString({Format}) Mechanism.

```    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Debug.Print(FormatDecimalValue("123456789.123456789"))
End Sub
```

Input:
123456789.123456789
Output:
123,456,789.123456789

Now my function isn't just doing a .ToString("N9"), it works out if the string being passed in is a decimal first, and then works out how many decimal places it has, so it can format it to the correct number of decimal places. But this is basic string manipulation, how many characters are after the point, String.Split the string on the "." and deal with the second section, then when you call .ToString("N{DecimalPrecision}").

Don't have the decimal as a string in the first place only as a decimal value? then just call .ToString() with no format string this will convert your decimal to a string so you can do a string.split.

### #7 Sheepings

• Senior Programmer

Reputation: 171
• Posts: 1,026
• Joined: 05-December 13

## Re: How Can I Format Decimal Number ?

Posted 21 December 2018 - 07:16 AM

@vedikaledange This is not a C# forum, OP is looking for help with VB.NET, hence why they posted it in the VB forums. Posting alternate languages just confuses people looking for help. Also, can you kindly post code between the code tags button, as this helps keep the formatting, as well as makes it readable.

@OP should disregard above post, unless you have an understanding for c#