Click here to Skip to main content
15,846,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am having a small conversion issue for Decimal datatyoe in VB.Net

I am having a variable declared as decimal
dim var as deciaml 
 var= 12341234123412.341

now I need to remove this fraction value and convert it into a whole number. I mean the result I want is 12341234123412341 ( only dot is removed and the 3 digits after dot are retained.)

even if I have the value var= 1234.1, I want the result as 1234100 ( after dot 3 digits sholud come and added to whole number). I know that I can do this by multiplying with 1000 but I want other possibilitis to achive this.

need help urgently.. Thanks
VJ Reddy 23-May-12 4:38am    
Thank you for accepting the solution :)

I know nothing better than multiply by 1000. Why don't you like that?

The following code
Dim v As Decimal = 100.5
v = v * 1000
Dim s As String = v.ToString("F0")


Share this answer
derek9999 21-May-12 6:02am    
I agree with CPallini. Unless you want to get bogged down with string formatting then multiplying by 1000 is the way to go.
Naveen_143 21-May-12 6:46am    
When i am multiplying the decimal variable with 1000, in window server with .netframe work, .000 is getting appended to the value. I mean if var = 100.5 and when i am doing var = var * 1000 gives me 100500.000 and when I am converting this to string the value is becoming 100500.000 which is an unwanted result for me. The end result i want is 100500 only as a string.
Naveen_143 21-May-12 7:28am    
I can remove the anwanted result from stirng but i want to know any direct method to do the above function.
VJ Reddy 21-May-12 8:20am    
Good answer. 5!
CPallini 21-May-12 8:29am    
Thank you.
Answer 1 by CPallini is good.

In case if you want to convert without multiplying with 1000, then I think the number can be first converted to String representation using F3 format and then the . may be replaced with "" as shown below
Dim var as Decimal =  1234.1D
Dim varString as String = var.ToString("F3").Replace(".","")

Share this answer
Maciej Los 21-May-12 8:41am    
Good answer, my 5!
VJ Reddy 21-May-12 8:42am    
Thank you, losmac :)
All other solutions other than multiplying by 1000 are going to be much slower, so if you need to do this lots and lots of times, that's your best method.
Share this answer

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