Results 1 to 10 of 10

Thread: Display 145 as 145.0

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    71

    Display 145 as 145.0

    Hi guys,

    I have to reduce a value like 145.0 to 5.0
    So I only need the last three digits including the "."
    Now with some simple
    Code:
    Dim strSubstr As String
    strSubstr = Right$(SWValue, 3)
    SWValue = strSubstr
    This is half solved.
    However the original SWValue as it arrives in this part of the code is not 145.0 but as 145 so this results in a wrong display after the Right$.
    When the value is 145.1 the code works fine.

    How can I display a figure always with a decimal even when that is 0 ...> 0.0

    Any guidenace in the correct direction would be swell.

    Paco

  2. #2
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,453

    Re: Display 145 as 145.0

    No idea what "I have to reduce a value like 145.0 to 5.0" really means, but why not:

    Code:
    SWValue = CLng(SWValue) Mod 10
    ... and call it good?

    But if you want to fiddle around with String expressions instead you might look at the Format$() function.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    71

    Re: Display 145 as 145.0

    Thanks for the suggestion about format$().
    That doesnot work I tried that already.

    The other solution doesnt work either.

    But I will explain more to get the idea behind it.

    I have a double declared value called ForceFinal
    Code:
    ForceFinal = Format(CDbl(ValueOut) - CDbl(txtZero0.Text), "0.0")
    I show this value in a text box and it shows when the value is 0 as 0.
    But I need it to show as 0,0
    So this is my needed base.
    In an calculation I have to determen the SWValue
    Now when the value is for example 145 it should show 145,0
    For the purpose of the program everything only the last three digits are needed to show.
    So from this 145,0 I need only the 5,0 to be displayed or worked with in other parts of the code.

    I hope my problem is more clear.

    Paco

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,972

    Re: Display 145 as 145.0

    1) Use Format$ not Format.
    2) What is ForceFinal dimmed as? If it's not a string, then it should be and could be the source of the issues.
    3) after clearing up #2, revisit #1.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    71

    Re: Display 145 as 145.0

    TG

    1] Format$ gave same result.

    Code:
    ForceFinal = Format$(CDbl(ValueOut) - CDbl(txtZero0.Text), "0.0")
    2] ForceFinal is dimmed as Double

    Paco

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,453

    Re: Display 145 as 145.0

    No, it is still very murky.

    If ForceFinal is Double then you don't want to play with Format$() except to format as a String purely for display. So something like this makes more sense:

    Code:
    SomeTextBox.Text = Format$(CDbl(ValueOut) - CDbl(tztZero0.Text), "0.0")
    That other part about "three digits" (I see two digits and the decimal point) is confusing too. Is that also only for display or do you need to get the altered value as a Double?

    Maybe:

    Code:
    SomeDouble = CDbl(CLng(Int((CDbl(ValueOut) - CDbl(txtZero0.Text)) * 10)) Mod 100) / 10
    Text1.Text = Format$(SomeDouble, "0.0")

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    71

    Re: Display 145 as 145.0

    Dilletante,

    For me three digits is like you described two digits and one decimal point.
    So the ForceFinal should as double when it is zero be 0.0 (take care the text box shows the decimal point as a comma)
    I need the ForceFinal for calculation and for a textbox.

    Maybe this part of code shows more of the back ground.
    In the end the SW1.text is a composure of a letter and a cut off value

    Letter A and the value 145.0 becomes A5.0

    Code:
    ForceFinal = Format$(CDbl(ValueOut) - CDbl(txtZero0.Text), "0.0") 'the final value of the load cell corrected for zero off setting
    
    If ForceFinal > 1 Or ForceFinal < -1 Then 'zero point deadband
    ForceFinal = ForceFinal
    Else
    ForceFinal = 0
    End If
    
    
    If ForceFinal < 0 Then 'make sure reading is always positive
    ForceFinal = ForceFinal * -1
    End If
    
    txtFinalValue.Text = ForceFinal 'display value as is
    
    'maximum force value
    If ForceFinal > ForceFinalMax Then
        ForceFinalMax = ForceFinal
    End If
    txtMaxForce = ForceFinalMax
    
    SWValue = ((ForceFinal - 325) / 3.5714) 'calculate SWvalue
    If SWValue <= ((325 - 325) / 3.5714) Then 'do not allow negative value
    SWValue = 0
    End If
    
    SWValue = Format(CDbl(SWValue), "####.0") 'show only one decimal digit
    
    If SWValue >= 0.1 Then
    SW2.Text = "A"
    End If
    
    If SWValue > 10 Then
    SW2.Text = "B"
    End If
    
    If SWValue > 20 Then
    SW2.Text = "C"
    End If
    
    If SWValue >= 30 Then
    SW2.Text = "D"
    End If
    
    If SWValue >= 40 Then
    SW2.Text = "E"
    End If
    
    If SWValue >= 50 Then
    SW2.Text = "F"
    End If
    
    If SWValue >= 60 Then
    SW2.Text = "G"
    End If
    
    If SWValue >= 70 Then
    'SW2.Text = "Overload"
    'SWValue = 0
    End If
    
    If SWValue = 0 Then
    SW2.Text = "A"
    End If
    
    Dim strSubstr As String
    strSubstr = Right$(SWValue, 3)
    SWValue = strSubstr
    
    SW1.Text = SW2.Text & " " & SWValue

    All values are dimmed as double

    Paco

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,972

    Re: Display 145 as 145.0

    Then that's your problem... you're confusing the value (145) with the display (145.0) ... the ONLY way to preserve that value is to TREAT IT AS A STRING when you display it. Use format$ to coerce it into a string, take the right 3 characters, and post pend them to your other variable...
    Code:
    Dim strSubstr As String
    strSubstr = Right$(format$(SWValue, "0.0"), 3)
    SW1.Text = SW2.Text & " " & strSubstr
    You can even condense it down more and save yourself a variable:
    Code:
    SW1.Text = SW2.Text & " " & Right$(format$(SWValue, "0.0"), 3)

    btw: this line - SWValue = Format(CDbl(SWValue), "####.0") 'show only one decimal digit -- useless.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    71

    Re: Display 145 as 145.0

    TG,

    That TREAT it as a string was the trick to display and calculate correctly.

    btw if I remove this line SWValue = Format(CDbl(SWValue), "####.0") 'show only one decimal digit
    then I only see the wholenumbers change and not the decimal figures. How odd it made sound.

    Paco

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,288

    Re: Display 145 as 145.0

    Hmm Not quite sure what is being said in post #9. If you want to show more than one decimal digit then you need to format it accordingly

    "0.0" Tells it to always display a leading 0 on numbers that are less than 1 and always display the trailing 0 even when it is a whole number. If you are only taking the Right$() 3 characters from that then you will always end up with a whole number a decimal point and the tenths value. If you want two decimal places to show then you need to add another 0 on the end and if you want more than the 3 characters to show then you need to use something other than 3 on your right() statement. the decimal point counts as a character btw

    Sounds like you need to read up on Format$() Right$() Strings and Numbers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width