Click to See Complete Forum and Search --> : FormatNumber!! Not working correctly...


GremlinSA
January 23rd, 2009, 04:38 AM
Here is another one for the books ...

I use FormatCurrency, and FormatNumber for the GUI data entry modules...

In .Lostfocus I use FormatCurrency, and in .Gotfocus i use FormatNumber..

Private Sub TxtAmounts_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtMaxBulk.GotFocus, TxtMaxSale.GotFocus _
, TxtMinSale.GotFocus, TxtComm.GotFocus
Dim TmpTxtBox As TextBox = CType(sender, TextBox)

If TmpTxtBox.Text <> "" And Not TmpTxtBox.ReadOnly Then TmpTxtBox.Text = FormatNumber(TmpTxtBox.Text) ' Error here
End Sub
Private Sub TxtCredits_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtMaxBulk.LostFocus, TxtMaxSale.LostFocus _
, TxtMinSale.LostFocus
Dim TmpVal As Double
Dim TmpTxtBox As TextBox = CType(sender, TextBox)
If TmpTxtBox.Text = "" Then
TmpVal = 0
Else
TmpVal = FormatNumber(TmpTxtBox.Text)
End If
TmpTxtBox.Text = FormatCurrency(TmpVal)
End Sub

However I now need to use a Percentage ..
and if i Use FormatPercent .. FormatNumber does not work .. Private Sub Txtpercent_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxtComm.LostFocus
Dim TmpVal As Double
Dim TmpTxtBox As TextBox = CType(sender, TextBox)
If TmpTxtBox.Text = "" Then
TmpVal = 0
Else
TmpVal = FormatNumber(TmpTxtBox.Text)
End If
TmpTxtBox.Text = FormatPercent(TmpVal , 0, TriState.True, TriState.False, TriState.False)
End Sub

I simply get Conversion from string "0%" to type 'Double' is not valid.

I thought that FormatNumber is supposed to return the numeric in any number format ... Why cant it handle percents.. :confused::(:cry::mad:

Gremmy..

TT(n)
January 23rd, 2009, 05:27 AM
This posts reminds me of another bug I found with TimeSpan that requires a workaround, ... but anyways.

FormatNumber returns a string though, so why are you setting it to a double?
Is this a Strict or Explicit project?
TmpVal = CDbl(FormatNumber(TmpTxtBox.Text))
Although it does say:
Returns an expression formatted as a number.
I guess they mean string expression.

FormatPercent does too:
Returns an expression formatted as a percentage (that is, multiplied by 100) with a trailing % character.

The only other thing I can think of is that your regional settings may be causing an issue.

GremlinSA
January 23rd, 2009, 06:04 AM
even when i tried with TmpString = FormatPercent(0.1, 0)
Dim tmpval As String = FormatNumber(TmpString)
i still get the same error ...System.InvalidCastException was unhandled
Message="Conversion from string "10%" to type 'Double' is not valid."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.Strings.FormatNumber(Object Expression, Int32 NumDigitsAfterDecimal, TriState IncludeLeadingDigit, TriState UseParensForNegativeNumbers, TriState GroupDigits)
at CashFlow3_Vendors.FrmUtility.TxtAmounts_GotFocus(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnGotFocus(EventArgs e)
---SNIP----
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.FormatException
Message="Input string was not in a correct format."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
at Microsoft.VisualBasic.CompilerServices.Conversions.ToDouble(String Value, NumberFormatInfo NumberFormat)
InnerException:


I'm using the Double type with FormatCurrency, and FormatPercent, not with the FormatNumber... It seams that Internally FormatNumber uses the Double type...

I've checked the reginal setings, and everything look right there..

Also i have to rely on reginal settings because the application will be used in several country's, and each has there own methods of displaying currencies, and i dont feel like writing code to Format and Unformat each different currency..

Gremmy..

GremlinSA
January 23rd, 2009, 06:11 AM
Ive resorted to writing a wrapper function to find the percents and format them appropriately ..

Public Shared Function FormatNum(ByVal Expresion As String) As String
If Expresion <> "" Then
If Expresion.EndsWith("%") Then
Return FormatNumber(Expresion.Substring(0, Expresion.Length - 1), 0)
End If
Return FormatNumber(Expresion, -1)
Else
Return "0"
End If
End Function


This works .. but it's not right ...

TT(n)
January 23rd, 2009, 06:25 AM
I get the same error in your example, but I didn't expect it to take a percent sign as a valid number format(expression). I see. It should be able to accept percents, and format them as a number, but I don't see any examples online that do that.

You're right it's not.

Private Function FormatNum(ByVal oString As Object) As String
If oString.ToString.IndexOf("%") <> -1 Then
oString = oString.ToString.Replace("%", "")
Return (CDbl(oString) / 100).ToString
End If
Return FormatNumber(oString)
End Function

GremlinSA
January 23rd, 2009, 06:54 AM
I'm following very strict rules for entry, NOTHING is left to chance. The entry module is going to be used by the biggest of idiot's you can find.

Every entry has a keycode filter on it so that no stray keypresses can slip in. also the client's requirement is to have all currency's etc, to be displayed properly formatted at all times.. (except when editing the value)
Hence the need to format & strip formating, in the LostFocus & GotFocus ...

However i always thought that Percentage was a numeric format .. Go figure..

TT(n)
January 23rd, 2009, 07:00 AM
Your intention was to convert percentage to decimal right?
Then make sure to divide by 100, unless you just want to drop the "%", in your function.

I think percents are a numeric format in excel or other MS apps, so you're right in that way I believe.

GremlinSA
January 23rd, 2009, 07:36 AM
haha .. remember,I said 'used by the biggest of idiot's you can find' ..

Do you think they will know that 0.1 is 10%.. they will punch in 10 and expect it to be 10% not 1000%.. (

So I have to compensate for this in the "Txtpercent_LostFocus" sub with..
TmpTxtBox.Text = FormatPercent((TmpVal / 100))
at least this way i can also ignore decimal inputs, less to cause problems. we only interrested in whole percentages..

BTW. Your sample is alot safer than mine, I'll feel better using it..