Limit numbers into 2 decimal places-VBForums

# Thread: Limit numbers into 2 decimal places

1. ## Limit numbers into 2 decimal places

hi!

someone help me how to limit number into 2 decimal places

sample when i got number of 1000.7585545 is should be 1000.76

i have this code below but based on the given sample it will result to 1001.00

Code:
```    If txtpassPrice.Text <> "" Then
Dim dblTranspo As Double
dblTranspo = txtpassPrice.Text
txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
End If```
i tried this one
but i will result to 1001 only
Code:
```    If txtpassPrice.Text <> "" Then
Dim dblTranspo As Double
dblTranspo = txtpassPrice.Text
txtpassPrice.Text = Format(dblTranspo, "#,##0.##")
End If```

2. ## Re: Limit numbers into 2 decimal places

The code you posted first should have given the correct display.

3. ## Re: Limit numbers into 2 decimal places

For go this method

vb Code:
`dblTranspo.ToString("N2")`

4. ## Re: Limit numbers into 2 decimal places

thanks for the responce opus but the first code i've displayed does not have a correct output... this is the result of the first code

sample when i got number of 1000.7585545 what i want to is 1000.76
but the result is 1001.00 and that is wrong...

5. ## Re: Limit numbers into 2 decimal places

Originally Posted by danasegarane
For go this method

vb Code:
`dblTranspo.ToString("N2")`
Have you tried the above one ?

6. ## Re: Limit numbers into 2 decimal places

danasegarane thanks for responce what is the N2?

7. ## Re: Limit numbers into 2 decimal places

it's the equivalent of the format command. It's an ability of the tostring.

i dont think the problem in your original code is with the rounding. what do you get when you do this?
Code:
`msgbox(dbltranspo)`
i would bet you are missing the decimal.
now this is from memory, but i am betting this is the problem:
Code:
`dblTranspo = txtpassPrice.Text`
just because dbltranspo is a double, doesn't mean converting from a string will give you a double to put in it. try this instead:
Code:
`dblTranspo = cdbl(txtpassPrice.Text)`
as another possibility, it may be losing the end of the string after the period due to "regional settings". If it expects a comma to be the decimal separator, it will ignore everything past a period.

8. ## Re: Limit numbers into 2 decimal places

i have tried the two of the code below but it gived me nothing happened, there is no changes happened..
Code:
``` If txtpassPrice..Text <> "" Then
Dim dblTranspo As Double
dblTranspo = txtpassPrice..Text
MsgBox(dblTranspo)
End If

If txtpassPrice..Text <> "" Then
Dim dblTranspo2 As Double
dblTranspo2 = txtpassPrice..Text
dblTranspo2 = CDbl(txtpassPrice..Text)
MsgBox(dblTranspo2)
End If```

9. ## Re: Limit numbers into 2 decimal places

Code:
``` Double db = 1000;
//Get the globalization info for US
System.Globalization.NumberFormatInfo ninfo = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
//Set the Number of digits
ninfo.NumberDecimalDigits = 2;
//Format as number ..
//with two decimal points
Response.Write(db.ToString("N",ninfo));
db=1000.000512;
//set the digits as 4
ninfo.NumberDecimalDigits=4;
Response.Write(db.ToString("N",ninfo));```
Outputs

Code:
```1,000.00
1,000.0005```
Where N points to Number Formating

10. ## Re: Limit numbers into 2 decimal places

what did you get from the message box? did it show correctly or were you already missing the digits?

11. ## Re: Limit numbers into 2 decimal places

thanks for the result danasegarane but i don't need the below result..

1,000.00
1,000.0005

but i need this result:1000.76 based on the given numbers in post # 1

12. ## Re: Limit numbers into 2 decimal places

thanks Lord Orwell, i got the same value as what is found in the textbox there is 100&#37; no changes happened..

13. ## Re: Limit numbers into 2 decimal places

Originally Posted by edgarbenilde
thanks for the result danasegarane but i don't need the below result..

1,000.00
1,000.0005

but i need this result:1000.76 based on the given numbers in post # 1
You need to try something by changing the example given. This worked for me

Code:
```Double db = 1000;
//Get the globalization info for US
System.Globalization.NumberFormatInfo ninfo = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
//Set the Number of digits
db = 1000.7585545;
//set the digits as 2
ninfo.NumberDecimalDigits = 2;
Response.Write(db.ToString("N", ninfo));```
Just change the NumberofDecimalDigits

14. ## Re: Limit numbers into 2 decimal places

danasegarane what is ninfo?

15. ## Re: Limit numbers into 2 decimal places

My problem is that I do not get the result you are getting.
I do use "1000.7585545" for input an get "1000.76".
I can only guess.
Are you using any code in the TextBox-Change event (like rounding..)?
Does the TextBox have a limited length?
What is the value of dblTranspo before you hand it over to the Textbox (use a messagebox to show it)?

16. ## Re: Limit numbers into 2 decimal places

thanks to opus my first code was working enough i just missing a line of code (the bold one)..

here is my final code, it's working 100&#37;

Code:
```        If txtpassPrice.Text <> "" Then
Dim dblTranspo As Double
dblTranspo = txtpassPrice.Text
txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
Me.txtpassPrice.Text = Me.txtpassPrice.Text
End If```

17. ## Re: Limit numbers into 2 decimal places

Originally Posted by edgarbenilde
thanks to opus my first code was working enough i just missing a line of code (the bold one)..

here is my final code, it's working 100%

Code:
```        If txtpassPrice.Text <> "" Then
Dim dblTranspo As Double
dblTranspo = txtpassPrice.Text
txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
Me.txtpassPrice.Text = Me.txtpassPrice.Text
End If```
That just doesn't make any sense - how can setting something to itself possibly change it's value?

18. ## Re: Limit numbers into 2 decimal places

Originally Posted by edgarbenilde
thanks to opus my first code was working enough i just missing a line of code (the bold one)..

here is my final code, it's working 100%

Code:
```        If txtpassPrice.Text <> "" Then
Dim dblTranspo As Double
dblTranspo = txtpassPrice.Text
txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
Me.txtpassPrice.Text = Me.txtpassPrice.Text
End If```
I have no idea why you did THAT change, nor do I have an idea why it is working for you now!

19. ## Re: Limit numbers into 2 decimal places

I guess the method 1 was working as intended. It is just that he didn't update the label text where he expected to see his change. I guess everyone who tried to help overestimated the OPs troubleshooting abilities, and assumed that he'd have the common sense of using message boxes or break points.

20. ## Re: Limit numbers into 2 decimal places

it is working actually i just happened that i forgot to update the value inside the textbox after the value is change... you try on this sample 1000.7585545 and the result should be 1000.76, so it's perfect..
Code:
```   If txtpassPrice.Text <> "" Then
Dim dblTranspo As Double
dblTranspo = txtpassPrice.Text
txtpassPrice.Text = Format(dblTranspo, "#,##0.00")
Me.txtpassPrice.Text = Me.txtpassPrice.Text
End If```

21. ## Re: Limit numbers into 2 decimal places

in that case this line is completely and irrevocably unnecessary.
Me.txtpassPrice.Text = Me.txtpassPrice.Text

22. ## Re: Limit numbers into 2 decimal places

How about we grab the data from the user logically first then display the value with only two decimal places?
This is assuming you're using VS 2005 and up:
Code:
```If txtpassPrice.Text <> String.Empty Then
Dim dblTranspo As Double
If Double.TryParse(txtpassPrice.Text.Trim, dblTranspo) = True Then
txtpassPrice.Text = dblTranspo.ToString("n2")
Else
txtpassPrice.Text = "Not a valid number!"
End If
End If```
And if you're using VS 2008 and up you can do this:
Code:
```If txtpassPrice.Text <> String.Empty Then
Dim dblTranspo As Double
txtpassPrice.Text = If(Double.TryParse(txtpassPrice.Text.Trim, dblTranspo) = True, dblTranspo.ToString("n2"), "Not a valid number!")
End If```

23. ## Re: Limit numbers into 2 decimal places

thanks you very much JuggaloBrotha your code is alos usefull..

24. ## Re: Limit numbers into 2 decimal places

Lord if i did not made it it will not give the result i have that is my way... will very thankful for who give the right idea..
Code:
```in that case this line is completely and irrevocably unnecessary.
Me.txtpassPrice.Text = Me.txtpassPrice.Text```

25. ## Re: Limit numbers into 2 decimal places

to those who give idea thank you all of you will get points..

26. ## Re: Limit numbers into 2 decimal places

the only way that line could possibly be doing anything is if you have an event triggered when the text is updated.

Assigning a value to something that already has that value doesn't change that value in any shape or form. You really need to upload the entire form's code so we can see what's really going on.

27. ## Re: Limit numbers into 2 decimal places

if my post is usefull to anybody please also rate my post

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured