eliminare numeri dopo la virgola

Discussione su Microsoft .NET Framework, C#, C++.NET, VB.NET, ASP.NET.

Moderatori: Toki, Rey, fonzarelli

eliminare numeri dopo la virgola

Messaggioda lillos » 08 giu 2008, 14:33

problemino nel codice sottostante io faccio la media del valore di 31 textbox inserendo il risultato nella textbox188. e fin li nessun problema.
il fatto e che io vorrei che il risultato deve avere solo un numero dopo la virgola. e non riesco
con due textbox e semplice ma con 31?
ecco il codice

Codice:
Private Sub sommamedia()
Try
Dim ahyp As Integer = Integer.Parse(TextBox4.Text)
Me.TextBox188.Text = (ahyp)

Dim bhyp As Integer = Integer.Parse(TextBox10.Text)
Me.TextBox188.Text = (ahyp + bhyp) / 2

Dim chyp As Integer = Integer.Parse(TextBox16.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp) / 3

Dim dhyp As String = Integer.Parse(TextBox22.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp) / 4

Dim eehyp As String = Integer.Parse(TextBox28.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp) / 5

Dim fhyp As String = Integer.Parse(TextBox34.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp) / 6

Dim ghyp As String = Integer.Parse(TextBox40.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp) / 7

Dim hhyp As String = Integer.Parse(TextBox46.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp) / 8

Dim ihyp As String = Integer.Parse(TextBox52.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp) / 9

Dim lhyp As String = Integer.Parse(TextBox58.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp) / 10

Dim mhyp As String = Integer.Parse(TextBox64.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp) / 11

Dim nhyp As String = Integer.Parse(TextBox70.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp) / 12


Dim ohyp As String = Integer.Parse(TextBox76.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp) / 13


Dim phyp As String = Integer.Parse(TextBox82.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp) / 14


Dim qhyp As String = Integer.Parse(TextBox88.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp) / 15


Dim rhyp As String = Integer.Parse(TextBox94.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp) / 16


Dim shyp As String = Integer.Parse(TextBox100.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp) / 17


Dim thyp As String = Integer.Parse(TextBox106.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp) / 18


Dim uhyp As String = Integer.Parse(TextBox112.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp) / 19


Dim vhyp As String = Integer.Parse(TextBox118.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp) / 20

Dim zhyp As String = Integer.Parse(TextBox124.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp) / 21

Dim khyp As String = Integer.Parse(TextBox130.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp) / 22

Dim xhyp As String = Integer.Parse(TextBox136.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp) / 23

Dim jhyp As String = Integer.Parse(TextBox142.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp) / 24

Dim aahyp As String = Integer.Parse(TextBox148.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp + aahyp) / 25

Dim abhyp As String = Integer.Parse(TextBox154.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp + aahyp + abhyp) / 26

Dim achyp As String = Integer.Parse(TextBox160.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp + aahyp + abhyp + achyp) / 27

Dim adhyp As String = Integer.Parse(TextBox166.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp + aahyp + abhyp + achyp + adhyp) / 28

Dim aehyp As String = Integer.Parse(TextBox172.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp + aahyp + abhyp + achyp + adhyp + aehyp) / 29

Dim afhyp As String = Integer.Parse(TextBox178.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp + aahyp + abhyp + achyp + adhyp + aehyp + afhyp) / 30

Dim aghyp As String = Integer.Parse(TextBox184.Text)
Me.TextBox188.Text = (ahyp + bhyp + chyp + dhyp + eehyp + fhyp + ghyp + hhyp + ihyp + lhyp + mhyp + nhyp + ohyp + phyp + qhyp + rhyp + shyp + thyp + uhyp + vhyp + zhyp + khyp + xhyp + jhyp + aahyp + abhyp + achyp + adhyp + aehyp + afhyp + aghyp) / 31



Catch
End Try

End Sub


come devo fare?
lillos
Utente Junior
 
Messaggi: 23
Iscritto il: 16 mag 2008, 20:25

Re: eliminare numeri dopo la virgola

Messaggioda cenere » 09 giu 2008, 16:53

ciao,
hai provato con un casting forzato o con un format del numero - stringa ?
Avatar utente
cenere
Saggio
 
Messaggi: 336
Iscritto il: 24 apr 2007, 14:52

Re: eliminare numeri dopo la virgola

Messaggioda lillos » 09 giu 2008, 18:42

purtroppo e da poco che studio vb.net e non saprei proprio come usare "casting forzato o con un format del numero - stringa ?"
mi spiegheresti come si fa ho un esempio
grazie
lillos
Utente Junior
 
Messaggi: 23
Iscritto il: 16 mag 2008, 20:25

Re: eliminare numeri dopo la virgola

Messaggioda Balamb Seed » 22 gen 2010, 10:36

io farei così

dim risultato as integer
risultato=text1.text+text2.text................text31.text
risultato=risultato/31
txttotale.text=risultato

in pratica se fai una variabile integer in cui fai i conti invece di usare la textbox nel momento in cui fai la divisione il sistema arrotonda in automatico

poi prendi il valore e lo metti nella tectbox del totale
Balamb Seed
Utente Junior
 
Messaggi: 79
Iscritto il: 21 dic 2006, 20:59

Re: eliminare numeri dopo la virgola

Messaggioda fzannino » 22 gen 2010, 18:13

Innanzitutto penso che se vuoi sola cifra dopo la virgola il risultato deve stare in un float
non in un integer;

Comunque prova ad usare la funzione round
Math.Round(3.44, 1) ti da 3.4.
Math.Round(3.45, 1) ti da 3.4.
Math.Round(3.46, 1) ti da 3.5.

"Math.Round" ha molti overloadings che ti possono sicuramente servire.
fzannino
Utente Junior
 
Messaggi: 52
Iscritto il: 15 gen 2010, 15:22


Torna a .NET Framework

Chi c’è in linea

Visitano il forum: MSN [Bot] e 4 ospiti

IPROGRAMMATORI.IT 2010. Tutti i diritti riservati. - P.IVA 04135460873 | Pubblicità | Termini e Condizioni - Privacy Policy | RSS 2.0 - Le news di IProgrammatori.itRSS