如何将文本框值转换为十进制(3,2) [英] How to Convert Textbox value to decimal(3,2)

查看:97
本文介绍了如何将文本框值转换为十进制(3,2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文本框值转换为十进制(3,2),如3.22,4.33等。

i尝试将文本框值除以100,但它将十进制值转换为0喜欢123它存储1.00但我希望1.23存储在db。



请帮忙。

解决方案

没有 decimal(3,2)之类的东西。看起来你混淆了不同的东西:数据,数字类型和数字数据的字符串表示。您需要处理数据,而不是字符串。并且不要将字符串放在可以使用数字类型的数据库中。如果你这样做,你需要修复数据库架构。



当你在屏幕上的某个地方出现问题时,问题只是十进制数据的正式问题。请参阅:

http:// msdn .microsoft.com / zh-CN / library / 364x0z75%28v = vs.80%29.aspx [ ^ ]。



用于十进制的字符串格式值,请参阅:

http://msdn.microsoft .com / zh-cn / library / system.decimal.tostring.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ],

http://msdn.microsoft.com/en-us/l ibrary / 0c899ak8.aspx [ ^ ]。



-SA


如果将文本框值除以100 123将因整数除法显示为1.00要解决此问题,您应该遵循以下代码: -

  double  val = 537 / 100f;  //   f告诉编译器执行浮动划分 
Console.WriteLine(val.ToString(< span class =code-string> #0.00));





如果这可以解决您的问题,请回复。 :)


这种方式也可以解决这个问题...

  string  s =(Convert.ToDouble(textbox1.Text)/  100 )。ToString(  0.00); 



快乐编码!

:)


I want to convert the text box value to decimal(3,2) like 3.22 ,4.33, etc
i tried to divide the text box value by 100 but it is converting the decimal values to 0 like for 123 it is storing 1.00 but i want 1.23 to be stored in db.

please help.

解决方案

There is no such thing as decimal(3,2) or something like that. It looks like you mix up different things: data, numeric types, and string representation of numeric data. You need to work with data, not strings. And don''t put strings in a database where you can use numeric types. If you do it, you need to fix the database schema.

You problem is merely a problem of formally of decimal data when you present it somewhere on the screen. Please see:
http://msdn.microsoft.com/en-us/library/364x0z75%28v=vs.80%29.aspx[^].

For string formatting of decimal values, please see:
http://msdn.microsoft.com/en-us/library/system.decimal.tostring.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA


If you divide the textbox value by 100 123 will be shown as 1.00 because of integer division. To solve this you should follow the below code:-

double val = 537/100f; // f tells compiler to perform float division
Console.WriteLine(val.ToString("#0.00"));



Reply if this solves your problem. :)


this way also, this problem can be solved...

string s = (Convert.ToDouble(textbox1.Text) / 100).ToString("0.00");


Happy Coding!
:)


这篇关于如何将文本框值转换为十进制(3,2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆