Solved

Truncating a double into its integer and decimal parts.

Posted on 2006-10-26
9
885 Views
Last Modified: 2008-01-09
vb2005

i need to perform a very basic thing: i have a Double variable called ReadHours which stores an amount of hours.
Suppose the variable is now holding the value 1.89
i am trying to extract the value 1 (the integer part of the Double, without rounding) on a different variable, and also the value 0.89 on another (the fractional value after i extracted the integer part).

for the life of me, ive been trying for the past 2 hours! does any of you have any ideas on this one? id rather not use Visual Basic namespace at all.

BTW, as clarification, i am trying to present my user with a readable hour and minute value rather than the 1.89 which is unreadable in terms of span. if i could achieve this separation i stated before, i could just present the integer part as the hours, and the fractional part * 60 as the minutes. ill give the points too if somebody has ideas on a better approach to this, maybe there is some hidden class that will do this for me...

thanks!!
0
Comment
Question by:newyuppie
9 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 17813571
Load the double into a decimal, and then you can use Decimal.Truncate to get the integer portion.  Subtract that from the original to get the fractional part.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17813572
Can it really be this easy?

        Dim myVal As Double = 1.89

        Dim strVal As String() = myVal.ToString.Split(".")

        Dim myInt As Integer = CInt(strVal(0))
        Dim MyDec As Decimal = CDec("0." & strVal(1))

        Debug.WriteLine(myInt)
        Debug.WriteLine(MyDec)

Roger
0
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 17813811
Here's yet another method yielding the same results:

            Dim ReadHours As Double = 1.89

            Dim myInt As Integer = CInt(ReadHours.ToString.Substring(0, ReadHours.ToString.IndexOf(".")))
            Dim myDec As Decimal = ReadHours - CDec(myInt)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 21

Expert Comment

by:mastoo
ID: 17814029
It's cleaner to avoid strings.  For example a double holding a big number has a ToString that looks like "1E+15".  So splitting or parsing, as Sancler and ZeonFlash suggest, falls apart.
0
 
LVL 13

Author Comment

by:newyuppie
ID: 17814115
mastoo, i guess you have a point there, i dont like the ToString method either.

i have devised this strategy relying on TimeSpan class, please may i have any thougts  on this technique, whether you think its viable or could return errors on occasion:

Private Function HoursToTicks(ByVal hours As Double) As ULong
        Dim _CONSTANT As ULong = 36000000000   ' number of ticks in 1 hour
        Return hours * _CONSTANT
    End Function

Dim TotalTicks As ULong = HoursToTicks(ReadHours)
        Dim ts As New TimeSpan(TotalTicks)

now i can use ts.Hours, ts.Minutes, etc and until now its giving correct results.
0
 
LVL 21

Assisted Solution

by:mastoo
mastoo earned 250 total points
ID: 17814273
I like it.  My only trivial point would be that you should use long instead of ULong as that is what the TimeSpan works with (and TotalTicks could be negative if you were looking at hours in the past).
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 17814292
I like the TimeSpan approach but would clean it up like so:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myVal As Double = 1.89
        Dim ts As TimeSpan = TimeSpanFromHours(myVal)
        Debug.Print(ts.Hours)
        Debug.Print(ts.Minutes)
    End Sub

    Private Function TimeSpanFromHours(ByVal hours As Double) As TimeSpan
        Return New TimeSpan(hours * TimeSpan.TicksPerHour)
    End Function
0
 
LVL 13

Author Comment

by:newyuppie
ID: 17814338
thanks for the suggestion. im giving you the points anyway for your suggestions and the non string approach
0
 
LVL 13

Author Comment

by:newyuppie
ID: 17814365
i just read Idle_Mind's post after i posted my comment. i love clean code! i hadnt idea the constants were already thought about and classified. im giving the points to mastoo and Idle_Mind\

thanks to all for your help
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.net Open Folder in Windows Explorer 3 51
export data table to excel in vb.net 10 36
Access Schema 6 24
vb.net convert long time to mm:ss 23 12
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question