6 Replies - 308 Views - Last Post: 21 December 2018 - 07:16 AM Rate Topic: -----

#1 Nicomendox   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • 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..
Was This Post Helpful? 1
  • +
  • -

#4 Nicomendox   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 ?
Was This Post Helpful? 0
  • +
  • -

#5 dbasnett   User is offline

  • D.I.C Addict
  • member icon

Reputation: 121
  • View blog
  • 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")


Was This Post Helpful? 0
  • +
  • -

#6 maceysoftware   User is offline

  • Foo
  • member icon

Reputation: 367
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • 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#
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1