• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Formating String values

My return value instead of looking like '1.00" it looks like "1"

How can I return it in the form "1.00"


CType(readingsOnScreen(intloop), Object).Text = tmpsaved.Value
0
cmdolcet
Asked:
cmdolcet
  • 10
  • 10
  • 2
1 Solution
 
rettiseertCommented:
string.Format("{0:N}",1)
0
 
rettiseertCommented:
or...

format(1,"0.00")
0
 
cmdolcetAuthor Commented:
If I add that code in the returned value is all "1.00" this is not waht I want What I need is for the numbers being return display past the "."

 CType(readingsOnScreen(intloop), Object).Text = tmpsaved.Value.Format("{0:N}", 1)
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Jorge PaulinoIT Pro/DeveloperCommented:
Try this way
 
 

        Dim value As Integer = 1
        Debug.WriteLine(value.ToString("0.00"))

Open in new window

0
 
cmdolcetAuthor Commented:
I get the following error when i put in the code up top:
 Error Target - ToSingle
    Error Message - Conversion from string "L" to type 'Single' is not valid.
    Exception Type - System.FormatException: Input string was not in a correct format.
   at Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToSingle(String Value, NumberFormatInfo NumberFormat)

Can anyone point me to the right direction



Public Class cSavedData
    Public Characteristic As String
    Public IsCalc As Boolean
    Public Value As String
    Public Index As Integer
    Public Passed As Boolean
    Public COMPort As String
 
 
Dim tmpsaved As cSavedData
CType(readingsOnScreen(intloop), Object).Text = tmpsaved.Value
                                    tmpsaved.Value = ToString("0.00")

Open in new window

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
It should be
tmpsaved.Value = tmpsaved.Value.ToString("0.00")

 
0
 
cmdolcetAuthor Commented:
I know get this error:

Error Information
    Error Target - LoadSavedData
    Error Message - Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.
    Exception Type - System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.
   at LMIObjectLibrary.NewRuntimeMethods.LoadSavedData(ArrayList charsOnScreen, ArrayList& readingsOnScreen)
Stack Trace
CType(readingsOnScreen(intloop), Object).Text = tmpsaved.Value
tmpsaved.Value = tmpsaved.Value.ToString("0.00")

Open in new window

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
But the error is not in the conversion!
 
Try to do this way to see if it works:
CType(readingsOnScreen(intloop), Object).Text = "1.00"
0
 
cmdolcetAuthor Commented:
It shows all 1.00 on the screen

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
That's because the you're working with strings and not numbers
Try this way:

            Dim str As String = "1"
            Dim value As Integer
            If Integer.TryParse(str, value) Then
                Debug.WriteLine(value.ToString("0.00"))
            End If

Open in new window

0
 
cmdolcetAuthor Commented:
Ok that code in your last response worked. It still gave me only 1.00
have do I get the actual value?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Try this:
            Dim str As String = tmpsaved.Value
            Dim value As Integer
            If Integer.TryParse(str, value) Then
                tmpsaved.Value = value.ToString("0.00")
            End If

Open in new window

0
 
cmdolcetAuthor Commented:
It doesn;t go into the:
If Integer.TryParse(str, value) Then
                tmpsaved.Value = value.ToString("0.00")
            End If


loop??????
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Can you show how you use it ?
0
 
cmdolcetAuthor Commented:
What do you mean I showed how I used it above?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
What do you mean with "It doesn;t go into the" ? I don't understand that!
 
0
 
cmdolcetAuthor Commented:
The code below. it doesn;t go into the  loop:
 tmpsaved.Value = value.ToString("0.00")

it just steps over the condition completely

 Dim str As String = tmpsaved.Value
            Dim value As Integer
            If Integer.TryParse(str, value) Then
                tmpsaved.Value = value.ToString("0.00")
            End If

Open in new window

0
 
Jorge PaulinoIT Pro/DeveloperCommented:
That's because it's not a valid integer value.
What value are you using ?
0
 
cmdolcetAuthor Commented:
Im using the tmpsaved.Value which is a string value. however if i use the .tostring ("0.00") it will jsut return the 0.00 on my screen?
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
>> Im using the tmpsaved.Value which is a string value
Ok, but what value ... give an example.
0
 
cmdolcetAuthor Commented:
the value is 0.01 and sometimes the value is 0.3333333333 it just depends on what things read

but in any event i want it to display 0.01 or 0.33  I only want the 2 decimal palces
0
 
Jorge PaulinoIT Pro/DeveloperCommented:
Ok, Gotcha!
Integer values only allow non decimal numbers. You have to use Decimal

            Dim str As String = tmpsaved.Value
            Dim value As Decimal
            If Decimal.TryParse(str, value) Then
                tmpsaved.Value = value.ToString("0.00")
            End If

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 10
  • 10
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now