-
May 26th, 2009, 01:39 AM
#1
Thread Starter
Fanatic Member
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
Please help me... how could i get the value 1000.76
thanks in advance
-
May 26th, 2009, 01:55 AM
#2
Re: Limit numbers into 2 decimal places
The code you posted first should have given the correct display.
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
May 26th, 2009, 02:02 AM
#3
Re: Limit numbers into 2 decimal places
For go this method
vb Code:
dblTranspo.ToString("N2")
Please mark you thread resolved using the Thread Tools as shown
-
May 26th, 2009, 03:14 AM
#4
Thread Starter
Fanatic Member
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...
-
May 26th, 2009, 03:18 AM
#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 ?
Please mark you thread resolved using the Thread Tools as shown
-
May 26th, 2009, 03:21 AM
#6
Thread Starter
Fanatic Member
Re: Limit numbers into 2 decimal places
danasegarane thanks for responce what is the N2?
-
May 26th, 2009, 03:31 AM
#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?
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.
Last edited by Lord Orwell; May 26th, 2009 at 03:33 AM.
Reason: added a 2nd solution
-
May 26th, 2009, 03:41 AM
#8
Thread Starter
Fanatic Member
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
-
May 26th, 2009, 04:04 AM
#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
Please mark you thread resolved using the Thread Tools as shown
-
May 26th, 2009, 04:11 AM
#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?
-
May 26th, 2009, 04:19 AM
#11
Thread Starter
Fanatic Member
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
-
May 26th, 2009, 04:20 AM
#12
Thread Starter
Fanatic Member
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% no changes happened..
-
May 26th, 2009, 04:28 AM
#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
Please mark you thread resolved using the Thread Tools as shown
-
May 26th, 2009, 04:32 AM
#14
Thread Starter
Fanatic Member
Re: Limit numbers into 2 decimal places
danasegarane what is ninfo?
-
May 26th, 2009, 04:33 AM
#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)?
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
May 26th, 2009, 06:07 AM
#16
Thread Starter
Fanatic Member
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%
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
-
May 26th, 2009, 06:46 AM
#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?
-
May 26th, 2009, 06:53 AM
#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!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
May 26th, 2009, 07:02 AM
#19
Hyperactive Member
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.
Runesmith
The key to getting the right answer is asking the right question
I just realized: good health is merely the slowest possible rate at which one can die
-
May 26th, 2009, 07:17 AM
#20
Thread Starter
Fanatic Member
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
-
May 26th, 2009, 12:28 PM
#21
Re: Limit numbers into 2 decimal places
in that case this line is completely and irrevocably unnecessary.
Me.txtpassPrice.Text = Me.txtpassPrice.Text
-
May 26th, 2009, 12:59 PM
#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
-
May 27th, 2009, 12:26 AM
#23
Thread Starter
Fanatic Member
Re: Limit numbers into 2 decimal places
thanks you very much JuggaloBrotha your code is alos usefull..
-
May 27th, 2009, 12:28 AM
#24
Thread Starter
Fanatic Member
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
-
May 27th, 2009, 12:33 AM
#25
Thread Starter
Fanatic Member
Re: Limit numbers into 2 decimal places
to those who give idea thank you all of you will get points..
-
May 27th, 2009, 01:58 AM
#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.
Last edited by Lord Orwell; May 27th, 2009 at 02:35 AM.
-
May 27th, 2009, 08:04 AM
#27
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|