none
issue using math.abs RRS feed

  • Question

  • i am trying to use math.abs but it not working 

    like 

    i have two value that give result in minus (-1000) 

    now im using math.abs and it removing the Minus sign but in database it saving the result without minus sign

    what is need is i want it to show a result without minus sign to user but in database it save it with minus sign

    is it possible ?

    Tuesday, January 21, 2020 9:49 AM

All replies

  • Hi,
    you can use binding with formatting like in demo:

    Public Class Form1
    
      Private tb As New TextBox With {.Dock = DockStyle.Top}
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(tb)
    
        Dim d1 As Double = -1000
        ' save to database with minus
        '
        ' read from database (with minus) to property
        Me.D2 = -1000
        '
        ' bind to Textbox with formatting
        Dim b As New Binding("Text", Me, "D2")
        AddHandler b.Format, AddressOf b_Format
        tb.DataBindings.Add(b)
      End Sub
    
      Private Sub b_Format(sender As Object, e As ConvertEventArgs)
        e.Value = Math.Abs(e.Value)
      End Sub
    
      Public Property D2 As Double
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, January 21, 2020 10:44 AM
  • thank you for the reply 

    my textbox name is openingbalance

    so how i can use it in your code ?

    Tuesday, January 21, 2020 10:53 AM
  • Hi,
    change tb to openingbalance.

    Public Class Form1
    
      Private openingbalance As New TextBox With {.Dock = DockStyle.Top}
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.Add(tb)
    
        Dim d1 As Double = -1000
        ' save to database with minus
        '
        ' read from database (with minus) to property
        Me.D2 = -1000
        '
        ' bind to Textbox with formatting
        Dim b As New Binding("Text", Me, "D2")
        AddHandler b.Format, AddressOf b_Format
        openingbalance.DataBindings.Add(b)
      End Sub
    
      Private Sub b_Format(sender As Object, e As ConvertEventArgs)
        e.Value = Math.Abs(e.Value)
      End Sub
    
      Public Property D2 As Double
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, January 21, 2020 11:50 AM
  • The following will convert a negative integer to a positive integer

    Dim value As Integer = -1000
    value = value * -1
    Console.WriteLine(value)

    Show to user, keep value

    Dim value As Integer = -1000
    Dim forUsers = (value * -1)
    TextBox1.Text = forUsers.ToString()


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Tuesday, January 21, 2020 2:45 PM
    Moderator
  • i am trying to use math.abs but it not working 

    like 

    i have two value that give result in minus (-1000) 

    now im using math.abs and it removing the Minus sign but in database it saving the result without minus sign

    what is need is i want it to show a result without minus sign to user but in database it save it with minus sign

    is it possible ?

    What data type are you using to store this in the DB?

    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Tuesday, January 21, 2020 4:37 PM
  • Do you also allow the user to edit these values?

    You can keep the original sign in a variable, like ‘sign1 = Math.Sign(value1)’, where sign1 is an Integer variable or member. Then display Math.Abs(value1).

    After editing the unsigned values, reapply the sign: ‘value1 *= If(sign1 = 0, 1, sign1)’. Then save to database.

    • Edited by Viorel_MVP Tuesday, January 21, 2020 6:37 PM
    Tuesday, January 21, 2020 6:26 PM