English 中文(简体)
格式 努力达到1个排位,不四舍五入/缩小
原标题:Formatting String to 1 Decimal Place, without rounding up/down
  • 时间:2011-09-05 17:34:44
  •  标签:
  • c#
  • string

我希望能发挥一种激动作用,如果这种愤怒超过某种价值(就我目前的情况而言是1 000人),那么我就希望对其作出某些区分,以便我最终能够恢复原始愤怒的缩略语。

For example:

1000 = 1
1001 = 1
1099 = 1
1100 = 1.1
1199 = 1.1
1200 = 1.2
10000 = 10
10099 = 10
10100 = 10.1

It s the division and rounding side of things that has been causing me problems.

给我带来上述结果的最适当方法是什么?

最佳回答

如何:

int dividedBy100 = x / 100; // Deliberately an integer division
decimal dividedBy1000 = dividedBy100 / 10m; // Decimal division
string result = dividedBy1000.ToString();

我将使用<条码>标准<>。 页: 1

问题回答

缩略语按定义四舍五入。

如果你想更精确地说明为什么不使用双倍而不是惯性?

这里的建议

double function(int number)
{
    if (number >= 1000)
    {
       return (((double)number) / 1000);
    }

}

你的事例似乎暗示,你只想有一个精度的模棱两可的地方,这样就怎么说:

  1. Divide by 100
  2. Cast to double (or float)
  3. Divide by 10

第一个司将缩小任何低于100号的拖车数量(相当于100个基底功能),然后将翻一番,然后将10个线分开,这将给你希望的单一精准地点。

如果是原始编号,则

int a=t/100

float b=a/10

b 包括答复

Some more code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                string s;
                s = Console.ReadLine();
                int a = Convert.ToInt32(s);
                a = a / 100;
                float b = a / (float)10.0;
                Console.WriteLine(b);
            }
        }
    }
}

You should use modular (remainder) mathematics to do this. You don t need to involve the FPU (Floating Point Unit).

static string RoundAndToString(int value, int denominator)
{
    var remainder = value % denominator;
    value = (value - remainder) / denominator;
    if (remainder == 0)
        return value.ToString();
    remainder = (remainder * 10) / denominator;
    return string.Format("{0}{1}{2}", value, CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, remainder);
}

既然你只是想缩减人数,那么,把它改为扼杀,从扼杀中删除最后两个特征,然后把10个字分开,以获得相应的数字。

这里是Ruby的算法。 (无C# 手提)

a = 1000 #sample number
 -> 1000

b = a.to_s[0..-3] #a converted to a string, then taking all characters except the last two.
 -> "10"

c = b.to_i / 10.0 # converts to float in correct power
 -> 1.0

You then display "c" in whatever format you want using sprintf (or the C# equivalent using FormatNumber).

审判

int MyNumber = 10100;

string MyString = ((int) MyNumber/1000).ToString() + (( MyNumber % 1000) > 99 ? "." + (((int)( MyNumber / 100 )) % 10).ToString() : "");




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签