Solved

Adding Decimal To A Number

Posted on 2000-05-04
17
170 Views
Last Modified: 2010-05-02
I have a number say 123456 and I want to add 2 decimals places to make it 1234.56. How do I do that??? Any ideas.(by the way-I wanna add more then 2 decimal places, please just give me the general idea)
0
Comment
Question by:the_chosen_one
  • 6
  • 4
  • 2
  • +5
17 Comments
 

Author Comment

by:the_chosen_one
ID: 2777063
And oh yeah, the user inputs the number in a text box and the program outputs it to the same text box
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 2777081
dim lngStartNumber as long
dim sngEndNumber as Single

lngStartNumber = txtTextBox.Text
sngEndNumber = CSng(lngStartNumber) /100

txtTextBox.Text = sngEndNumber
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 2777099
Or, try this

dim intLength as Integer
dim strMantissa as String
dim strDecimal as String

intLength = Len(txtTextBox.Text)
strMantissa = left$(txtTextBox.Text, intLength-2)
strDecimal = Right$(txtTextBox.Text, 2)
txtTextBox.Text = strMantissa & "." & strDecimal
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 2777101
You could, before either of those examples, use an InStr(txtTextBox.Text, ".") to see if there is already a decimal in place.
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2777105
Divide it by 10 ^ the number of places you want to add (2 places = 10^3, 3 places = 10^3, etc.) If the "number" is a string use VAL to convert it into a real number. If you're having trouble *displaying* the number use the FORMAT(myNumber, "###.##") command and simply change the number of #'s to the right of the decimal.

M
0
 
LVL 18

Expert Comment

by:deighton
ID: 2777117
Here's how a code ninja would do it

Private Sub Command1_Click()
Text1 = add_dec(Text1, 2)
End Sub


Public Function add_dec(Number, nDecs As Long)

    add_dec = Format(Number / 10 ^ nDecs, "#." & String(nDecs, "0"))

End Function
0
 
LVL 14

Accepted Solution

by:
mcrider earned 70 total points
ID: 2777192
Deighton, there is a real problem with your code if the number already contains a decimal point...


Try this instead... It also allows you to return a leading zero if you want.


Public Function add_dec(ByVal Number, nDecs As Long, Optional UseLeadingZero As Boolean)
    Dim lString As String
    Dim iVal As Long
    lString = CStr(Number)
    iVal = InStr(1, lString, ".")
    If Not iVal = 0 Then Number = Number * 10 ^ (Len(lString) - iVal)
    add_dec = Format(Number / 10 ^ nDecs, IIf(UseLeadingZero, "0", "#") & "." & String(nDecs, "0"))
End Function



You would call it like this:

   Text1 = add_dec(Text1, 2,true)



Cheers!®©
0
 

Expert Comment

by:woodsrr
ID: 2777249
format("234",".00")
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 14

Expert Comment

by:mcrider
ID: 2777310
Woodsrr... format("123456",".00") will not get you 1234.56

Read the question again...
0
 
LVL 15

Expert Comment

by:ameba
ID: 2777358
I didn't get 1234.56 from any of the above mentioned solutions.

my 0,02 DEM
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2777424
ameba, If your system is not set for US currency, you're not gonna get 1234.56


My solutions works on all of my US systems... ;-)
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2777445
By the way if you want format 00,00 use this:

Public Function add_dec(ByVal Number, nDecs As Long, Optional UseLeadingZero As Boolean)
    Dim lString As String
    Dim iVal As Long
    lString = CStr(Number)
    iVal = InStr(1, lString, ",")
    If Not iVal = 0 Then Number = Number * 10 ^ (Len(lString) - iVal)
    lString = Format(Number / 10 ^ nDecs, IIf(UseLeadingZero, "0", "#") & "." & String(nDecs, "0"))
    Mid$(lString, InStr(1, lString, "."), 1) = ","
    add_dec = lString
End Function



Cheers!®©
0
 
LVL 1

Expert Comment

by:schrmar
ID: 2777451
Dim intDecimalPlaces As Integer
intDecimalPlaces = 2
If InStr(Text1.Text, ".") = 0 Then
   Text1.Text = Left(Text1.Text, Len(Text1.Text) - intDecimalPlaces) _
            & "." & Right(Text1.Text, intDecimalPlaces)
End If
0
 

Author Comment

by:the_chosen_one
ID: 2780699
Thanx mcrider, it helped a lot. Now I've gotta figure out how to put 0's if they don't have enough numbers but want decimal places.
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 2780773
You can do that through string parsing.

Say, at the end of all of the above, you end up with "1234.56" in your text box, but you want four decimal places.

intDecimals = 4      'Desired decimal places
intWhereDot = InStr(Text1.Text, ".")
Do While Len(Text1.Text) < (intWhereDot + intDecimals)
    Text1.Text = Text1.Text + "0"
Loop


Good luck!
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2782101
Thanks for the points! Glad I could help!...

Modifying the routine like this will allow you to specify leading zeros:

Public Function add_dec(ByVal Number, nDecs As Long, _
    Optional UseLeadingZero As Boolean, Optional nInts As Long)
    Dim lString As String
    Dim iVal As Long
    lString = CStr(Number)
    iVal = InStr(1, lString, ".")
    If Not iVal = 0 Then Number = Number * 10 ^ (Len(lString) - iVal)
    If nInts = 0 Then
        add_dec = Format(Number / 10 ^ nDecs, IIf(UseLeadingZero, "0", "#") & "." & String(nDecs, "0"))
    Else
        add_dec = Format(Number / 10 ^ nDecs, String(nInts, "0") & "." & String(nDecs, "0"))
    End If
End Function




Where:

    X = add_dec(100,3) 'RETURNS .100

    X = X = add_dec(100,3,,2) 'RETURNS 00.100


Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2782109
That should have been:

Where:

    X = add_dec(100,3) 'RETURNS .100

    X = add_dec(100,3,,2) 'RETURNS 00.100

Cut and paste.... Geez! ;-)


Cheers!®©

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA: Add rows to listbox based on criteria 7 74
Write data from SQL to CSV file - position 1 52
VB error "Type mismatch" 2 50
Modifying Conditional Format from VBA code 3 49
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now