Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > built in function that will grab the value of a number before and after a decimal
 

built in function that will grab the value of a number before and after a decimal

If I have a number 6.44

I want to take 6.44 and split it up into 2 variables

variable one = 6

variable two = .44

if I have a packaging qty that ends with 6.44 boxes, then I want to take .44 and multiply it by the box qty to get the qty of the last box


If there is no built in function can someone tell me how else I can do this?

NateZilla77  Tuesday, August 18, 2009 7:22 PM
            decimal d = 6.44m;

            decimal w = Math.Truncate(d);
            decimal f = Math.Abs(d - w);

            Console.WriteLine(w); // Prints 6
            Console.WriteLine(f); // Prints 0.44



(Note that if there is a negative sign, the following assigns it to the whole part only. Drop the Math.Abs() and the sign will be in both the whole and fractional parts.)

  • Marked As Answer byNateZilla77 Monday, August 24, 2009 1:25 PM
  •  
BinaryCoder  Wednesday, August 19, 2009 12:12 AM
            decimal d = 6.44m;

            decimal w = Math.Truncate(d);
            decimal f = Math.Abs(d - w);

            Console.WriteLine(w); // Prints 6
            Console.WriteLine(f); // Prints 0.44



(Note that if there is a negative sign, the following assigns it to the whole part only. Drop the Math.Abs() and the sign will be in both the whole and fractional parts.)

  • Marked As Answer byNateZilla77 Monday, August 24, 2009 1:25 PM
  •  
BinaryCoder  Wednesday, August 19, 2009 12:12 AM
Hello,

A decimal can be much greater than an integer, however if the decimal is not so big, you may also try like the following

decimal d = 6.44m;

int w = (int)d;
decimal f = d - w;

Console.WriteLine(w);
Console.WriteLine(f);

If you are using big numbers, please take BinaryCoder's idea

Let me know if you want VB.NET code.

Thanks,
Rong-Chun Zhang
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Wednesday, August 19, 2009 7:18 AM
Hello,

I am writing to check the status of the issue on your side. Would you mind letting me know the result of the suggestions? If you have any additional question, welcome to post here.

Have a great day!

Thanks,
Rong-Chun Zhang
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Rong-Chun Zhang  Monday, August 24, 2009 3:46 AM

You can use google to search for other answers

Custom Search

More Threads

• how do I conditionally change a DataGridViewButtonColumn?
• Data Column Collection
• DatagridComboBoxColumn
• DataGridView high CPU usage
• Programmatically add new row to DataGridView.
• POSTAL CODE
• DataColumnChanging problems...
• Combo Box problem
• VS.Net IDE - All Object Data Sources Gone
• Way to delete records in a Master-Details DataGridView