Problem numbers after the comma

Asked

Viewed 89 times

1

Hello I have this code on a button.

txtresultado.Text = Math.Round(txtsalmin.Text * 0.2, 2).ToString

Where it takes the value of salmin.text and multiplies poe 0.2 and appears in txtresultado.text with at most 2 boxes after the comma. And my problem is this, for example, if 937 * 0.2 is equal to 187.4 and this appears in txtresultado.text, so far everything is right, but I would like it to appear 187.40 because I am working with money in this system and it has to appear the 2 houses after the comma.

Someone could help me?

1 answer

0


You can specify the number of decimal places in the Tostring() method as follows:

txtresultado.Text = Math.Round(txtsalmin.Text * 0.2, 2).ToString.("N2")
txtresultado.Text = Math.Round(txtsalmin.Text * 0.2, 2).ToString.("F2")

Here is a small example

Imports System

Public Module modmain
   Sub Main()
       Console.WriteLine ( Math.Round(10937 * 0.2, 2).ToString())     
       Console.WriteLine ( Math.Round(10937 * 0.2, 2).ToString("N2"))   
       Console.WriteLine ( Math.Round(10937 * 0.2, 2).ToString("F2")) 
   End Sub
End Module

Upshot:

2187.4
2,187.40
2187.40
  • 1

    I got it, I didn’t know about that option. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.