Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a small program that generates a random decimal amount. I want to extract the leading whole number from the generated number, but keep the fractional part (".00").
I'd also like to keep this a decimal.

What I have tried:

I have tried just about every math function I can find in visual basic and on the web. I must be missing something.
Posted
Updated 20-Sep-18 20:14pm

What is wrong with floor?

After using it, you just have to format you number to have .00 decimals again and that's it
 
Share this answer
 
Comments
Maciej Los 21-Sep-18 2:14am    
5ed!
Nelek 21-Sep-18 11:40am    
thanks
[no name] 21-Sep-18 9:11am    
Thank you.
Nelek 21-Sep-18 11:39am    
You are welcome
Have yoi tried Decimal.Truncate(Decimal) Method (System) | Microsoft Docs[^] - that is what it is designed to do...
decimal d = 3.31M;
Console.WriteLine("{0}, {1}, {1:0.00}", d, decimal.Truncate(d));


Dim d As Decimal = 3.31D
Console.WriteLine("{0}, {1}, {1:0.00}", d, Decimal.Truncate(d))
 
Share this answer
 
v2
Comments
Maciej Los 21-Sep-18 2:14am    
5ed!
[no name] 21-Sep-18 9:12am    
Thank You.
OriginalGriff 21-Sep-18 9:59am    
You're welcome!
 
Share this answer
 
Comments
[no name] 21-Sep-18 9:12am    
Thank You.
Maciej Los 21-Sep-18 9:15am    
You're very welcome.

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